C# 为 WebClient 请求设置 User-Agent 标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11841540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the User-Agent header for a WebClient request
提问by AndreiC
What is the proper way of setting the User-Agent header for a WebClient request for Windows Phone 7? I found 2 options, but not sure which one is the correct one. Considering a WebClient object:
为 Windows Phone 7 的 WebClient 请求设置 User-Agent 标头的正确方法是什么?我找到了 2 个选项,但不确定哪一个是正确的。考虑一个 WebClient 对象:
WebClient client = new WebClient();
I saw 2 options:
我看到了 2 个选项:
set the User-Agent using:
client.Headers["User-Agent"] = "myUserAgentString";set the User-Agent using the WebHeaderCollection:
WebHeaderCollection headers = new WebHeaderCollection(); headers[HttpRequestHeader.UserAgent] = "userAgentString"; client.Headers = headers;
使用以下方法设置用户代理:
client.Headers["User-Agent"] = "myUserAgentString";使用 WebHeaderCollection 设置用户代理:
WebHeaderCollection headers = new WebHeaderCollection(); headers[HttpRequestHeader.UserAgent] = "userAgentString"; client.Headers = headers;
Can you please advise which of the 2 methods above is the proper one?
您能否告知上述两种方法中的哪一种是正确的?
采纳答案by Doc Roms
You can check the WebClientdocumentationfor a C# sample that adds a User-Agent to your WebClientand herefor a sample for Windows Phone.
您可以查看C# 示例的WebClient文档,该示例将用户代理添加到您的WebClient和此处以获取适用于 Windows Phone 的示例。
This is the sample for C#:
这是 C# 的示例:
WebClient client = new WebClient ();
// Add a user agent header in case the
// requested URI contains a query.
client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; " +
"Windows NT 5.2; .NET CLR 1.0.3705;)");
This is a sample for Windows Phone (Silverlight):
这是 Windows Phone (Silverlight) 的示例:
request.Headers["UserAgent"] = "appname";
// OR
request.UserAgent = "appname";
回答by Ambrose Leung
I found that the WebClient kept removing my User-Agent header after one request and I was tired of setting it each time. I used a hack to set the User-Agent permanently by making my own custom WebClient and overriding the GetWebRequestmethod. Hope this helps.
我发现 WebClient 在一个请求后不断删除我的 User-Agent 标头,我厌倦了每次设置它。我通过创建自己的自定义 WebClient 并覆盖 GetWebRequest方法来使用 hack 来永久设置用户代理。希望这可以帮助。
public class CustomWebClient : WebClient
{
public CustomWebClient(){}
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address) as HttpWebRequest;
request.UserAgent="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/6.0;)";
//... your other custom code...
return request;
}
}
回答by alabala
const string ua = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
Request.Headers["User-Agent"] = ua;
var httpWorkerRequestField = Request.GetType().GetField("_wr", BindingFlags.Instance | BindingFlags.NonPublic);
if (httpWorkerRequestField != null)
{
var httpWorkerRequest = httpWorkerRequestField.GetValue(Request);
var knownRequestHeadersField = httpWorkerRequest.GetType().GetField("_knownRequestHeaders", BindingFlags.Instance | BindingFlags.NonPublic);
if (knownRequestHeadersField != null)
{
string[] knownRequestHeaders = (string[])knownRequestHeadersField.GetValue(httpWorkerRequest);
knownRequestHeaders[39] = ua;
}
}
回答by DavidRR
As a supplement to the other answers, here is Microsoft's guidance for user agent stringsfor its browsers. The user agent strings differ by browser (Internet Explorer and Edge) and operating system (Windows 7, 8, 10 and Windows Phone).
作为对其他答案的补充,这里是微软对其浏览器的用户代理字符串的指导。用户代理字符串因浏览器(Internet Explorer 和 Edge)和操作系统(Windows 7、8、10 和 Windows Phone)而异。
For example, here is the user agent string for Internet Explorer 11 on Windows 10:
例如,以下是Windows 10 上 Internet Explorer 11的用户代理字符串:
Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
and for Internet Explorer for Windows Phone 8.1 Update:
和适用于 Windows Phone 8.1 更新的 Internet Explorer:
Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 4.0; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 520) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko) Mobile Safari/537
Templates are given for the user agent strings for the Edgebrowser for Desktop, Mobile and WebView. See this answerfor some Edge user agent string examples.
为桌面、移动和 WebView的Edge浏览器的用户代理字符串提供了模板。有关某些 Edge 用户代理字符串示例,请参阅此答案。
Finally, another page on MSDNprovides guidance for IE11 on older desktop operating systems.
最后,MSDN 上的另一个页面为旧桌面操作系统上的 IE11 提供了指导。
IE11 on Windows 8.1:
Windows 8.1 上的 IE11:
Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko
and IE11 on Windows 7:
和Windows 7 上的 IE11:
Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko
回答by Theodore Tsirpanis
You can also use that:
您也可以使用它:
client.Headers.Add(HttpRequestHeader.UserAgent, "My app.");
回答by Oscar Fraxedas
This worked for me:
这对我有用:
var message = new HttpRequestMessage(method, url);
message.Headers.TryAddWithoutValidation("user-agent", "<user agent header value>");
var client = new HttpClient();
var response = await client.SendAsync(message);
Here you can find the documentation for TryAddWithoutValidation
在这里您可以找到TryAddWithoutValidation的文档

