为WebClient请求设置User-Agent标头
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为WebClient请求设置User-Agent标头相关的知识,希望对你有一定的参考价值。
为Windows Phone 7的WebClient请求设置User-Agent标头的正确方法是什么?我找到了2个选项,但不确定哪一个是正确的选项。考虑WebClient对象:
WebClient client = new WebClient();
我看到了2个选项:
- 使用以下方式设置User-Agent:
client.Headers["User-Agent"] = "myUserAgentString";
- 使用WebHeaderCollection设置User-Agent:
WebHeaderCollection headers = new WebHeaderCollection(); headers[HttpRequestHeader.UserAgent] = "userAgentString"; client.Headers = headers;
你能否告诉我上面两种方法中哪一种是正确的?
您可以查看WebClient
documentation以获取C#示例,该示例将用户代理添加到WebClient
和here以获取Windows Phone示例。
这是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;)");
这是Windows Phone(Silverlight)的示例:
request.Headers["UserAgent"] = "appname";
// OR
request.UserAgent = "appname";
我发现WebClient在一次请求后不断删除我的User-Agent标头,我每次都厌倦了设置它。我使用hack通过创建自己的自定义WebClient并覆盖GetWebRequest方法来永久设置User-Agent。希望这可以帮助。
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;
}
}
你也可以使用它:
client.Headers.Add(HttpRequestHeader.UserAgent, "My app.");
作为其他答案的补充,这里是微软对user agent strings浏览器的指导。用户代理字符串因浏览器(Internet Explorer和Edge)和操作系统(Windows 7,8,10和Windows Phone)而异。
例如,以下是Windows 10上Internet Explorer 11的用户代理字符串:
Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
和Internet Explorer for Windows Phone 8.1更新:
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
为Desktop,Mobile和WebView的Edge浏览器的用户代理字符串提供了模板。有关Edge用户代理字符串示例,请参阅this answer。
最后,another page on MSDN为旧版桌面操作系统上的IE11提供了指导。
Windows 8.1上的IE11:
Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko
和Windows 7上的IE11:
Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko
这对我有用:
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);
在这里您可以找到TryAddWithoutValidation的文档
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;
}
}
以上是关于为WebClient请求设置User-Agent标头的主要内容,如果未能解决你的问题,请参考以下文章
Spring WebFlux WebClient 构建器设置请求正文