How to edit request header headers in C # via web request?

When I try to change the host key to Request.Headers in a console application, I get an exception that says:

The 'Host' header cannot be modified directly.
Parameter name: name

So how can I change it?

+3
source share
1 answer

As you saw .Net Fx does not allow editing the host header, but with .Net Fx 4.0 there is a separate definition for "Host" in the HttpWebRequest object. You can use it as follows:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://127.0.0.1/");
request.Host = "yourdomain.com";

Hope this helps you.

+3
source

All Articles