views:

927

answers:

3

I'm using the code below to pull one of our 3rd party developed pages in so I can parse it as XML for my random bits of work.

Irritatingly we stil have a browser detection level set on the server that only allows certain browsers on to the site; so the question is how would I fake it so that the server thinks its a browser request?

   static string GetHtmlPage(string strURL)
    {

        String strResult;
        System.Net.WebResponse objResponse;

        System.Net.WebRequest objRequest = System.Net.HttpWebRequest.Create(strURL);

        objResponse = objRequest.GetResponse();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(objResponse.GetResponseStream()))
        {
            strResult = sr.ReadToEnd();
            sr.Close();
        }
        return strResult;
    }
+1  A: 

I think most (if not all) browser detection is based on the User-Agent header, set by the HttpRequest.UserAgent property. I see there is a website for user-agent strings of various browsers: http://www.user-agents.org/

Daniel Pratt
Thanks that links really really handy :o)
Chris M
+6  A: 

Browser detection is done based on a header in the request to the server. All you need to do is set that header. However, with HttpWebRequest you don't set that through the headers collection but rather with the .UserAgent property.

...
System.Net.WebRequest objRequest = 
   System.Net.HttpWebRequest.Create(strURL);

//Pretend to be IE7
((System.Net.HttpWebRequest)objRequest).UserAgent = 
   "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)";

objResponse = objRequest.GetResponse();
...
WaldenL
Cracking but had to replace your objRequest.UserAgent with((System.Net.HttpWebRequest)objRequest).UserAgentAs it fell over the other way..Took this tiny bit of code from http://www.primaryobjects.com/CMS/Article64.aspx
Chris M
You're correct, I missed the fact that objRequest is a WebRequest, not an HttpWebRequest. I've modded the sample for the future.
WaldenL
Excellent, fooled the Traffic Scotland website nicely!
Jason
A: 

as with Waldens above but had to replace

objRequest.UserAgent = 
   "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)";

with

((System.Net.HttpWebRequest)objRequest).UserAgent = "Mozilla/5.0 (compatible; Googlebot/2.1; http://www.google.com/bot.html)";

Otherwise it fell over. (I changed the browser to googlebot to evade our cookie server)

Chris M