HttpWebRequest to pretend like a browser request?

2019-03-04 19:15发布

问题:

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

For some reason, it recently starts returning 500 Internal Error when requested from my app.
(The response contains some HTML for the navigations, but doesn't have the main content I need)

On Firefox/Chrome/IE, it is still returning 200 OK.

The problem is I don't have control over their code, I don't know what it does on the backend that causes it to break when requested from my app.

Is there a way I can "pretend" to make the request from, say, Google Chrome? (just to avoid the error)

回答1:

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