Faking browser request in 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# 中伪造浏览器请求


- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01