PageRenderTime 27ms CodeModel.GetById 11ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

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

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