设置WebClient请求的User-Agent标头


70

为Windows Phone 7的WebClient请求设置User-Agent标头的正确方法是什么?我找到2个选项,但不确定哪个是正确的。考虑一个WebClient对象:

WebClient client = new WebClient();

我看到2个选项:

  1. 使用以下方法设置用户代理:

    client.Headers["User-Agent"] = "myUserAgentString";
    
  2. 使用WebHeaderCollection设置用户代理:

    WebHeaderCollection headers = new WebHeaderCollection();
    headers[HttpRequestHeader.UserAgent] = "userAgentString";
    client.Headers = headers;
    

您能否建议上述两种方法中的哪一种是正确的?

Answers:


111

您可以查看WebClient文档的,增加了一个用户代理给您一个C#示例WebClient,并在这里为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";

24

我发现WebClient在一个请求后一直删除我的User-Agent标头,而每次设置我都很烦。我通过创建我自己的自定义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;
    }
}


2

作为其他答案的补充,这是Microsoft针对其浏览器的用户代理字符串的指南。用户代理字符串因浏览器(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中的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用户代理字符串示例,请参见此答案

最后,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

1

这为我工作:

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的文档


-2
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;
    }
}

4
仅代码不是答案,您能否解释它的作用以及为什么要使用它?
热雷米贝特朗
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.