为WebClient请求设置User-Agent标头

时间:2012-08-07 07:56:26

标签: c# windows-phone-7

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

WebClient client = new WebClient();

我看到了两个选项:

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

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

    WebHeaderCollection headers = new WebHeaderCollection();
    headers[HttpRequestHeader.UserAgent] = "userAgentString";
    client.Headers = headers;
    
  3. 您能否告知上述两种方法中哪一种是正确的?

6 个答案:

答案 0 :(得分:97)

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

答案 1 :(得分:22)

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

答案 2 :(得分:6)

您也可以使用:

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

答案 3 :(得分:2)

作为其他答案的补充,以下是Microsoft对其浏览器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 

适用于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

为桌面,移动和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

答案 4 :(得分:0)

这对我有用:

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

答案 5 :(得分:-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;
    }
}