Ejemplo n.º 1
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);
                }
            }
        }
Ejemplo n.º 2
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);
        }