How to set User-Agent in WebClient

∥☆過路亽.° 提交于 2019-12-18 09:28:25

问题


I used the code below to open a stream request to a youtube video, but it always return an exception "The remote server returned an error: NotFound". Then I tried to use Fiddler to detect the problem, and I saw that the WebClient auto set User-Agent field to NativeHost, not my User-Agent as below.

My code to send a request to 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;
}

Headers that were captured from 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

Please give me some advice. Many thanks


回答1:


You can use this code

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



回答2:


Try this for adding the User Agent

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



回答3:


You can do this as well.

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


来源:https://stackoverflow.com/questions/22656049/how-to-set-user-agent-in-webclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!