PageRenderTime 42ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Ping/Trackback.cs

#
C# | 121 lines | 72 code | 19 blank | 30 comment | 9 complexity | d5eeb00849d876b2b4d025cd6bf68dda MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace BlogEngine.Core.Ping
  2. {
  3. using System;
  4. using System.IO;
  5. using System.Net;
  6. /// <summary>
  7. /// The trackback.
  8. /// </summary>
  9. public static class Trackback
  10. {
  11. #region Events
  12. /// <summary>
  13. /// Occurs just before a trackback is sent.
  14. /// </summary>
  15. public static event EventHandler<EventArgs> Sending;
  16. /// <summary>
  17. /// Occurs when a trackback has been sent
  18. /// </summary>
  19. public static event EventHandler<EventArgs> Sent;
  20. #endregion
  21. #region Public Methods
  22. /// <summary>
  23. /// The send.
  24. /// </summary>
  25. /// <param name="message">
  26. /// </param>
  27. /// <returns>
  28. /// The send.
  29. /// </returns>
  30. public static bool Send(TrackbackMessage message)
  31. {
  32. if (!BlogSettings.Instance.EnableTrackBackSend)
  33. {
  34. return false;
  35. }
  36. if (message == null)
  37. {
  38. throw new ArgumentNullException("message");
  39. }
  40. OnSending(message.UrlToNotifyTrackback);
  41. // Warning:next line if for local debugging porpuse please donot remove it until you need to
  42. // tMessage.PostURL = new Uri("http://www.artinsoft.com/webmaster/trackback.html");
  43. var request = (HttpWebRequest)WebRequest.Create(message.UrlToNotifyTrackback);
  44. // HttpHelper.CreateRequest(trackBackItem);
  45. request.Credentials = CredentialCache.DefaultNetworkCredentials;
  46. request.Method = "POST";
  47. request.ContentLength = message.ToString().Length;
  48. request.ContentType = "application/x-www-form-urlencoded";
  49. request.KeepAlive = false;
  50. request.Timeout = 10000;
  51. using (var writer = new StreamWriter(request.GetRequestStream()))
  52. {
  53. writer.Write(message.ToString());
  54. }
  55. bool result;
  56. HttpWebResponse response;
  57. try
  58. {
  59. response = (HttpWebResponse)request.GetResponse();
  60. OnSent(message.UrlToNotifyTrackback);
  61. string answer;
  62. using (var sr = new StreamReader(response.GetResponseStream()))
  63. {
  64. answer = sr.ReadToEnd();
  65. }
  66. // TODO: This could be a strict XML parsing if necesary/maybe logging activity here too
  67. result = response.StatusCode == HttpStatusCode.OK && answer.Contains("<error>0</error>");
  68. }
  69. catch
  70. {
  71. // (WebException wex)
  72. result = false;
  73. }
  74. return result;
  75. }
  76. #endregion
  77. #region Methods
  78. /// <summary>
  79. /// Called when [sending].
  80. /// </summary>
  81. /// <param name="url">The URL.</param>
  82. private static void OnSending(Uri url)
  83. {
  84. if (Sending != null)
  85. {
  86. Sending(url, new EventArgs());
  87. }
  88. }
  89. /// <summary>
  90. /// Called when [sent].
  91. /// </summary>
  92. /// <param name="url">The URL.</param>
  93. private static void OnSent(Uri url)
  94. {
  95. if (Sent != null)
  96. {
  97. Sent(url, new EventArgs());
  98. }
  99. }
  100. #endregion
  101. }
  102. }