HttpWebRequest to pretend like a browser request?

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)

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply