mhinze.com archive

this is an archive of the old blog, ended 6/16/08





    06
    Nov

    HtmlAgilityPack User Agent / HttpWebRequest object

    using System;
    using HtmlAgilityPack;
    using System.Net;
    
    class LbHtmlWeb : HtmlWeb
    {
        private readonly string USER_AGENT =
            "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
    
        public LbHtmlWeb()
        {
            this.PreRequest += delegate(HttpWebRequest request)
            {
                request.UserAgent = USER_AGENT;
                return true;
            };
        }
    
        //…
    }

     

    Now that was easy!  Pretty spiffy that HtmlWeb exposes that event.

    Please let me at the HttpWebRequest.

    Don't do this (RSS Toolkit, I'm talking to you):

    XmlDocument doc = new XmlDocument();
    doc.Load(url);

     

    =(

    One Response to “HtmlAgilityPack User Agent / HttpWebRequest object”

    1. Week Link Post 15 « Rhonda Tipton’s WebLog Says:

      [...] Matt Hinze has posted code for a snazzy HtmlAgilityPack User Agent. [...]

    Leave a Reply

    © 2007 mhinze.com