Faking browser request in ASP.net C#

后端 未结 4 1138
自闭症患者
自闭症患者 2020-12-15 12:14

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 de

4条回答
  •  无人及你
    2020-12-15 13:02

    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();
    ...
    

提交回复
热议问题