PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Ping/Pingback.cs

#
C# | 165 lines | 102 code | 22 blank | 41 comment | 13 complexity | 2f921a0633179e1bfc0210c877178c23 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.Net;
  5. using System.Text;
  6. using System.Xml;
  7. /// <summary>
  8. /// Sends pingbacks to website that the blog links to.
  9. /// </summary>
  10. public static class Pingback
  11. {
  12. #region Events
  13. /// <summary>
  14. /// Occurs just before a pingback is sent.
  15. /// </summary>
  16. public static event EventHandler<EventArgs> Sending;
  17. /// <summary>
  18. /// Occurs when a pingback has been sent
  19. /// </summary>
  20. public static event EventHandler<EventArgs> Sent;
  21. #endregion
  22. #region Public Methods
  23. /// <summary>
  24. /// Sends pingbacks to the targetUrl.
  25. /// </summary>
  26. /// <param name="sourceUrl">
  27. /// The source Url.
  28. /// </param>
  29. /// <param name="targetUrl">
  30. /// The target Url.
  31. /// </param>
  32. public static void Send(Uri sourceUrl, Uri targetUrl)
  33. {
  34. if (!BlogSettings.Instance.EnablePingBackSend)
  35. {
  36. return;
  37. }
  38. if (sourceUrl == null || targetUrl == null)
  39. {
  40. return;
  41. }
  42. try
  43. {
  44. var request = (HttpWebRequest)WebRequest.Create(targetUrl);
  45. request.Credentials = CredentialCache.DefaultNetworkCredentials;
  46. var response = (HttpWebResponse)request.GetResponse();
  47. string pingUrl = null;
  48. var pingUrlKeyIndex = Array.FindIndex(
  49. response.Headers.AllKeys,
  50. delegate(string k)
  51. {
  52. return k.Equals("x-pingback", StringComparison.OrdinalIgnoreCase) ||
  53. k.Equals("pingback", StringComparison.OrdinalIgnoreCase);
  54. });
  55. if (pingUrlKeyIndex != -1)
  56. {
  57. pingUrl = response.Headers[pingUrlKeyIndex];
  58. }
  59. Uri url;
  60. if (!string.IsNullOrEmpty(pingUrl) && Uri.TryCreate(pingUrl, UriKind.Absolute, out url))
  61. {
  62. OnSending(url);
  63. request = (HttpWebRequest)WebRequest.Create(url);
  64. request.Method = "POST";
  65. // request.Timeout = 10000;
  66. request.ContentType = "text/xml";
  67. request.ProtocolVersion = HttpVersion.Version11;
  68. request.Headers["Accept-Language"] = "en-us";
  69. AddXmlToRequest(sourceUrl, targetUrl, request);
  70. var response2 = (HttpWebResponse)request.GetResponse();
  71. response2.Close();
  72. OnSent(url);
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. ex = new Exception();
  78. // Stops unhandled exceptions that can cause the app pool to recycle
  79. }
  80. }
  81. #endregion
  82. #region Methods
  83. /// <summary>
  84. /// Adds the XML to web request. The XML is the standard
  85. /// XML used by RPC-XML requests.
  86. /// </summary>
  87. /// <param name="sourceUrl">
  88. /// The source Url.
  89. /// </param>
  90. /// <param name="targetUrl">
  91. /// The target Url.
  92. /// </param>
  93. /// <param name="webreqPing">
  94. /// The webreq Ping.
  95. /// </param>
  96. private static void AddXmlToRequest(Uri sourceUrl, Uri targetUrl, HttpWebRequest webreqPing)
  97. {
  98. var stream = webreqPing.GetRequestStream();
  99. using (var writer = new XmlTextWriter(stream, Encoding.ASCII))
  100. {
  101. writer.WriteStartDocument(true);
  102. writer.WriteStartElement("methodCall");
  103. writer.WriteElementString("methodName", "pingback.ping");
  104. writer.WriteStartElement("params");
  105. writer.WriteStartElement("param");
  106. writer.WriteStartElement("value");
  107. writer.WriteElementString("string", sourceUrl.ToString());
  108. writer.WriteEndElement();
  109. writer.WriteEndElement();
  110. writer.WriteStartElement("param");
  111. writer.WriteStartElement("value");
  112. writer.WriteElementString("string", targetUrl.ToString());
  113. writer.WriteEndElement();
  114. writer.WriteEndElement();
  115. writer.WriteEndElement();
  116. writer.WriteEndElement();
  117. }
  118. }
  119. /// <summary>
  120. /// Called when [sending].
  121. /// </summary>
  122. /// <param name="url">The URL Uri.</param>
  123. private static void OnSending(Uri url)
  124. {
  125. if (Sending != null)
  126. {
  127. Sending(url, new EventArgs());
  128. }
  129. }
  130. /// <summary>
  131. /// Called when [sent].
  132. /// </summary>
  133. /// <param name="url">The URL Uri.</param>
  134. private static void OnSent(Uri url)
  135. {
  136. if (Sent != null)
  137. {
  138. Sent(url, new EventArgs());
  139. }
  140. }
  141. #endregion
  142. }
  143. }