如何在WebClient中设置User-Agent

时间:2014-03-26 09:08:32

标签: c# webclient user-agent

我使用下面的代码打开对youtube视频的流请求,但它总是返回异常“远程服务器返回错误:NotFound”。然后我尝试使用Fiddler来检测问题,我看到WebClient自动将User-Agent字段设置为NativeHost,而不是我的User-Agent,如下所示。

我向youtube发送请求的代码:

private static Task<string> HttpGet(string uri)
{
    var task = new TaskCompletionSource<string>();

    var web = new WebClient();
    web.OpenReadCompleted += (sender, args) =>
    {
        if (args.Cancelled)
            task.SetCanceled();
        else if (args.Error != null)
            task.SetException(args.Error);
        else
        {
            //var bytes = args.Result.ReadToEnd();
            byte[] bytes = new byte[] { };
            using (MemoryStream memoryStream = new MemoryStream())
            {
                args.Result.CopyTo(memoryStream);
                bytes = memoryStream.ToArray();

                task.SetResult(Encoding.UTF8.GetString(bytes, 0, bytes.Length));
            }
        }
    };

    web.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
    web.OpenReadAsync(new Uri(uri));

    return task.Task;
}

从Fiddler捕获的标题:

CONNECT www.youtube.com:443 HTTP/1.0
User-Agent: NativeHost
Host: www.youtube.com:443
Content-Length: 0
Connection: Keep-Alive
Pragma: no-cache

请给我一些建议。非常感谢

3 个答案:

答案 0 :(得分:4)

您可以使用此代码

using (WebClient web = new WebClient())
    {
    web.Headers["User-Agent"] =
    "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) " +
    "(compatible; MSIE 6.0; Windows NT 5.1; " +
    ".NET CLR 1.1.4322; .NET CLR 2.0.50727)";
      }

答案 1 :(得分:1)

尝试添加用户代理

web.Headers.Add("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")

答案 2 :(得分:0)

你也可以这样做。

using (WebClient webClient = new WebClient())
{
      webClient.Headers.Add("user-agent", "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Safari/537.36");
}