在 ASP.net C# 中伪造浏览器请求

Faking browser request in ASP.net C#(在 ASP.net C# 中伪造浏览器请求)

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

问题描述

我正在使用下面的代码来提取我们的第 3 方开发页面之一,以便我可以将其解析为 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 您不是通过 headers 集合设置它,而是使用 .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# 中伪造浏览器请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 ASP.net C# 中伪造浏览器请求

基础教程推荐