How to set User-Agent in WebClient

烂漫一生 提交于 2019-11-29 16:43:06

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

Try this for adding the User Agent

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

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