Esempio n. 1
0
        /// <summary>
        /// If the current site/link supports a TrackBack, we will ping the site here
        /// </summary>
        /// <param name="trackBackItem"></param>
        public bool SendTrackBackPing()
        {
            try
            {
                string trackBackItem = GetTrackBackText(_pageText, _externalUrl, _postLink);
                if (!string.IsNullOrEmpty(trackBackItem))
                {
                    if (!trackBackItem.ToLower().StartsWith("http://") && !trackBackItem.ToLower().StartsWith("https://"))
                    {
                        trackBackItem = "http://" + trackBackItem;
                    }

                    string parameters = "title=" + HttpUtility.UrlEncode(HttpUtility.HtmlDecode(_postTitle)) + "&url=" + HttpUtility.UrlEncode(_postLink) + "&blog_name=" + HttpUtility.UrlEncode(HttpUtility.HtmlDecode(_siteName)) + "&excerpt=" + HttpUtility.UrlEncode(_excerpt);
                    byte[] payload    = Encoding.UTF8.GetBytes(parameters);

                    HttpWebRequest request = GRequest.CreateRequest(trackBackItem, _postLink);
                    request.Method                       = "POST";
                    request.ContentLength                = payload.Length;
                    request.ContentType                  = "application/x-www-form-urlencoded";
                    request.KeepAlive                    = false;
                    request.AllowAutoRedirect            = true;
                    request.MaximumAutomaticRedirections = 3;

                    using (Stream st = request.GetRequestStream())
                    {
                        st.Write(payload, 0, payload.Length);
                        st.Close();

                        using (WebResponse response = request.GetResponse())
                        {
                            response.Close();
                        }
                    }

                    string message = String.Format("Trackback sent to {0}.", _externalUrl);
                    Log.Info("Trackback Sent", message);

                    return(true);
                }
            }
            catch (System.Exception ex)
            {
                string message = String.Format("Trackback attempt to {0} failed. Error message returned was: {1}.", _externalUrl, ex.Message);
                Log.Warn("Trackback Error", message);
            }

            return(false);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates an array of links based on the post's body. Then walks through each link and attempts to send trackbacks/pingbacks
        /// </summary>
        public void CheckPost(object state)
        {
            List <string> links = GetLinks(_postBody, _baseUrl);

            foreach (string externalUrl in links)
            {
                if (string.IsNullOrEmpty(externalUrl))
                {
                    continue;
                }

                try
                {
                    WebHeaderCollection headers = null;
                    string pageText             = null;

                    using (HttpWebResponse response = GRequest.GetResponse(externalUrl, _postLink))
                    {
                        headers  = response.Headers;
                        pageText = GRequest.GetPageText(response);
                        response.Close();
                    }

                    if (!string.IsNullOrEmpty(pageText))
                    {
                        bool pingSent = false;

                        // First try to send a TrackBack if the required RDF info was embedded in the HTML
                        TrackBackSender trackBack = new TrackBackSender(pageText, externalUrl, _siteName, _postTitle, _postLink, _excerpt);
                        pingSent = trackBack.SendTrackBackPing();

                        // If the Trackback attempt failed, try to do a Pingback
                        // (if the required PingBack XML-RPC service URL exists in the HTTP Header or HTML Head)
                        if (!pingSent)
                        {
                            PingBackSender pingBack = new PingBackSender(headers, pageText, externalUrl, _postTitle, _postLink);
                            pingSent = pingBack.SendPingbackPing();
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    string message = String.Format("Trackback/Pingback attempt to the url [{0}] failed for post {1} while retrieving the remote document. Error message returned was: {2}.", externalUrl, this._postTitle, ex.Message);
                    Log.Warn("Trackback/Pingback Error", message);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Retrieves a remote html page and determines if it contains a link to a given target url. Also gets the page title.
        /// </summary>
        internal static bool SourceContainsTarget(string sourceURI, string targetURI, out string pageTitle)
        {
            pageTitle = string.Empty;
            Uri    baseUri = new Uri(sourceURI);
            string baseUrl = string.Format("{0}://{1}{2}", baseUri.Scheme, baseUri.Host, baseUri.Port == 80 ? string.Empty : ":" + baseUri.Port.ToString());

            string         page    = null;
            HttpWebRequest request = GRequest.CreateSafeRequest(sourceURI, targetURI);

            request.MaximumAutomaticRedirections = 3;
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response != null)
                {
                    if (GRequest.IsContentTypeValid(response.ContentType, GRequest.ContentTypeCategory.HTML) && response.ContentLength < 300000)
                    {
                        page = GRequest.GetPageText(response);
                    }

                    response.Close();
                }
            }

            if (string.IsNullOrEmpty(page))
            {
                return(false);
            }

            if (page.IndexOf(targetURI) < 0)
            {
                // The direct link to the Graffiti post was not found in the body of the source post
                // BUT, the source may have linked to a url that redirected to the Graffiti post (i.e. Feedburner url)
                List <string> links           = LinkParser.GetLinks(page, baseUrl);
                bool          targetLinkFound = false;

                foreach (string externalUrl in links)
                {
                    if (string.IsNullOrEmpty(externalUrl))
                    {
                        continue;
                    }

                    try
                    {
                        string         responseUrl = null;
                        HttpWebRequest request2    = GRequest.CreateSafeRequest(externalUrl, targetURI);
                        request2.MaximumAutomaticRedirections = 3;
                        using (HttpWebResponse response2 = request2.GetResponse() as HttpWebResponse)
                        {
                            if (response2 != null)
                            {
                                if (GRequest.IsContentTypeValid(response2.ContentType, GRequest.ContentTypeCategory.HTML) && response2.ContentLength < 300000)
                                {
                                    responseUrl = response2.ResponseUri.ToString();
                                }

                                response2.Close();
                            }
                        }

                        if (responseUrl != null && string.Compare(targetURI, responseUrl, StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            targetLinkFound = true;
                            break;
                        }
                    }
                    catch { }
                }

                if (!targetLinkFound)
                {
                    return(false);
                }
            }

            // Look for the HTML Title of the remote page
            string pat = @"<head.*?>.*<title.*?>(.*)</title.*?>.*</head.*?>";
            Regex  reg = new Regex(pat, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            Match  m   = reg.Match(page);

            if (m.Success)
            {
                pageTitle = HttpUtility.HtmlDecode(Graffiti.Core.Util.RemoveHtml(m.Result("$1").Replace("\r", "").Replace("\n", "").Replace("\t", "").Trim(), 250));
            }

            return(true);
        }