为WebClient请求设置User-Agent标头

问题描述 投票:58回答:6

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

WebClient client = new WebClient();

我看到了2个选项:

  1. 使用以下方式设置User-Agent: client.Headers["User-Agent"] = "myUserAgentString";
  2. 使用WebHeaderCollection设置User-Agent: WebHeaderCollection headers = new WebHeaderCollection(); headers[HttpRequestHeader.UserAgent] = "userAgentString"; client.Headers = headers;

你能否告诉我上面两种方法中哪一种是正确的?

c# windows-phone-7
6个回答
101
投票

您可以查看WebClient documentation以获取C#示例,该示例将用户代理添加到WebClienthere以获取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";

23
投票

我发现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;
    }
}

8
投票

你也可以使用它:

client.Headers.Add(HttpRequestHeader.UserAgent, "My app.");

2
投票

作为其他答案的补充,这里是微软对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

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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.