HttpWebRequest to pretend like a browser request?

后端 未结 1 564
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-26 05:53

I have some code (in a Winform app) that reads this URL using HttpWebRequest.GetResponse().

For some reason, it recently starts returning 500

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-26 06:57

    Set the HttpWebRequest.UserAgent property to the value of a real browser's user agent.

    HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create("http://example.com");
    
    webRequest.UserAgent = @"Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36";
    

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