问题描述
我正在使用下面的代码来提取我们的第 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# 中伪造浏览器请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)