在ASP.net C#伪装浏览器请求 [英] Faking browser request in ASP.net C#

查看:1353
本文介绍了在ASP.net C#伪装浏览器请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code来拉我们的第三方的一个开发的,所以我可以解析为XML的工作我的随机位的网页。

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;
    }


推荐答案

浏览器检测是基于在请求到服务器的标题完成。所有你需要做的是设置标题。然而,随着HttpWebRequest的您没有设置通过头集合,而是与.UserAgent财产。

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

这篇关于在ASP.net C#伪装浏览器请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆