HttpWebRequest: how to identify as a browser?

前端 未结 1 1853
独厮守ぢ
独厮守ぢ 2021-01-04 02:55

The question is how to construct HttpWebRequest so queried server will think it comes from a browser?

1条回答
  •  星月不相逢
    2021-01-04 03:39

    You could set the User-Agent HTTP request header.

    var request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
    request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
    

    or if you work with a WebClient:

    using (var client = new WebClient())
    {
        client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
        ...
    }
    

    0 讨论(0)
提交回复
热议问题