PageRenderTime 28ms CodeModel.GetById 10ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

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

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