Exemplo n.º 1
0
        /// <summary>
        /// Sends a trackback message, and sees if te response is OK.
        /// </summary>
        /// <param name="message">The TrackbackMessage to send</param>
        /// <returns>A boolean value indicating if the message sending was successfull</returns>    
        public static bool Send(TrackbackMessage message)
        {
            if (!BlogSettings.Instance.EnableTrackBackSend)
                return false;

              if (message == null)
            throw new ArgumentNullException("message");

              OnSending(message.UrlToNotifyTrackback);
              HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(message.UrlToNotifyTrackback);
              request.Credentials = CredentialCache.DefaultNetworkCredentials;
              request.Method = "POST";
              request.ContentLength = message.ToString().Length;
              request.ContentType = "application/x-www-form-urlencoded";
              request.KeepAlive = false;
              request.Timeout = 10000;

              using (StreamWriter myWriter = new StreamWriter(request.GetRequestStream()))
              {
            myWriter.Write(message.ToString());
              }

              bool result = false;
              HttpWebResponse response;
              try
              {
            response = (HttpWebResponse)request.GetResponse();
            OnSent(message.UrlToNotifyTrackback);
            string answer;
            using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
            {
              answer = sr.ReadToEnd();
            }

            if (response.StatusCode == HttpStatusCode.OK)
            {
              //This could be a strict XML parsing if necessary maybe logging activity here too
              if (answer.Contains("<error>0</error>"))
              { result = true; }
              else
              { result = false; }
            }
            else
            { result = false; }
              }
              catch(Exception)
              {
            result = false;
              }
              return result;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sends the trackback or pingback message.
        /// <remarks>
        /// It will try to send a trackback message first, and if the refered web page
        /// doesn't support trackbacks, a pingback is sent.
        /// </remarks>
        /// <para>
        /// Gets all anchors from the content of the IShowed item and ignores links to ourselves.
        /// From all the rest of the links, we resolve them and check for trackback links,
        /// if present we send trackback, else we do a pingback.
        /// </para>
        /// </summary>
        public static void Send(IShowed item)
        {
            foreach (Uri url in GetUrlsFromContent(item.Content))
              {
                if (url.Host == SupportUtilities.AbsoluteWebRoot.Host)
                    continue;

                string pageContent = ReadFromWeb(url);
                Uri trackbackUrl = GetTrackBackUrlFromPage(pageContent);
                bool isTrackbackSent = false;

                if (trackbackUrl != null)
                {
                    TrackbackMessage message = new TrackbackMessage(item, trackbackUrl);
                    isTrackbackSent = Trackback.Send(message);
                }

                if (!isTrackbackSent)
                {
                    Pingback.Send(item.AbsoluteLink, url);
                }
              }
        }