简体   繁体   English

为WebClient请求设置User-Agent标头

[英]Setting the User-Agent header for a WebClient request

What is the proper way of setting the User-Agent header for a WebClient request for Windows Phone 7? 为Windows Phone 7的WebClient请求设置User-Agent标头的正确方法是什么? I found 2 options, but not sure which one is the correct one. 我找到了2个选项,但不确定哪一个是正确的选项。 Considering a WebClient object: 考虑WebClient对象:

WebClient client = new WebClient();

I saw 2 options: 我看到了2个选项:

  1. set the User-Agent using: 使用以下方式设置User-Agent:

     client.Headers["User-Agent"] = "myUserAgentString"; 
  2. set the User-Agent using the WebHeaderCollection: 使用WebHeaderCollection设置User-Agent:

     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? 你能否告诉我上面两种方法中哪一种是正确的?

You can check the WebClient documentation for a C# sample that adds a User-Agent to your WebClient and here for a sample for Windows Phone. 您可以检查WebClient文件的,增加了一个用户代理给您一个C#示例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";

I found that the WebClient kept removing my User-Agent header after one request and I was tired of setting it each time. 我发现WebClient在一次请求后不断删除我的User-Agent标头,我每次都厌倦了设置它。 I used a hack to set the User-Agent permanently by making my own custom WebClient and overriding the GetWebRequest method. 我使用hack通过创建自己的自定义WebClient并覆盖GetWebRequest方法来永久设置User-Agent。 Hope this helps. 希望这可以帮助。

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.");

As a supplement to the other answers, here is Microsoft's guidance for user agent strings for its browsers. 作为其他答案的补充,这里是Microsoft对其浏览器的用户代理字符串的指导。 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 : 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

Templates are given for the user agent strings for the Edge browser for Desktop, Mobile and WebView. 为Desktop,Mobile和WebView的Edge浏览器的用户代理字符串提供了模板。 See this answer for some Edge user agent string examples. 有关Edge用户代理字符串示例,请参阅此答案

Finally, another page on MSDN provides 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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM