[英]Error with WebHeaderCollection class when trying to add the header “User-Agent”
good morning, I need your help. 早上好,我需要你的帮助。
I have an error with WebHeaderCollection class when trying to add the header "User-Agent" jumping me the error in visual studio is as follows: 尝试添加标题“ User-Agent”时,WebHeaderCollection类出现错误,这使我在Visual Studio中的错误如下:
"This header must be modified With The right property
“必须使用right属性修改此标头
My code is as follows and the fault is on the third line. 我的代码如下,故障在第三行。
private static readonly WebHeaderCollection Headers = new WebHeaderCollection()
{
{"User-Agent", "Custom-User-Agent"}, // <<=== ERROR??
{"Cookie", "MyCookie"},
{"application", "netconnect"}
};
private static void Start(int nRequests)
{
WebRequest.DefaultWebProxy = null;
for (var i = 0; i < nRequests; ++i) {
SendRequest();
}
}
private static bool SendRequest()
{
var request = HttpWebRequest.Create("URL");
request.Headers = Headers;
using (var response = (HttpWebResponse)request.GetResponse()) {
//Returns boolean indicating success
return response.StatusCode == HttpStatusCode.OK;
}
}
Any solution to solve this? 有解决方案吗? Thank you very much!
非常感谢你!
Some common headers are considered restricted and are either exposed directly by the API (such as Content-Type) or protected by the system and cannot be changed.
一些常见的标头被认为是受限制的,或者直接由API(例如Content-Type)公开,或者由系统保护,并且无法更改。
You may specify UA on HttpWebRequest
: 您可以在
HttpWebRequest
上指定UA:
request.UserAgent = "Custom-User-Agent"
The following can be done: 可以完成以下操作:
new HttpResponse()
{
RequestUri = string.Empty,
StatusCode = HttpStatusCode.OK,
Cookies = new CookieCollection()
{
new Cookie("MyCookie", cookie)
},
Headers = new WebHeaderCollection()
{
"X-Powered-By:Servlet/3.1 JSP/2.3 (Payara Server 4.1.1.162 #badassfish Java/Oracle Corporation/1.8)"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.