Add request headers using WebClient C #

I have the following code with which I load a webpage into an array of bytes and then print it with Response.Write:

WebClient client = new WebClient(); byte[] data = client.DownloadData(requestUri); /*********** Init response headers ********/ WebHeaderCollection responseHeaders = client.ResponseHeaders; for (int i = 0; i < responseHeaders.Count; i++) { Response.Headers.Add(responseHeaders.GetKey(i), responseHeaders[i]); } /***************************************************/ 

Besides the response headers, I also need to add request headers. I am trying to do this with the following code:

  /*********** Init request headers ********/ NameValueCollection requestHeaders = Request.Headers; foreach (string key in requestHeaders) { client.Headers.Add(key, requestHeaders[key]); } /***************************************************/ 

However, this does not work, and I get the following exception:

This header should be modified using the appropriate property. Parameter Name: name

Can someone help me with this? What is the correct way to add request headers using WebClient?

Thanks.

+6
source share
2 answers

The collection of headers protects some of the possible headers, as described here on the msdn page: http://msdn.microsoft.com/en-us/library/system.net.webclient.headers.aspx

This page seems to give the whole answer you need, but to quote the important part:

Some common headers are considered restricted and protected and cannot be set or changed in the WebHeaderCollection object. Any attempt to set one of these restricted headers to the WebHeaderCollection associated with the WebClient will throw an exception later when trying to send a WebClient request.

Limited system-protected headers include, but are not limited to:

 Date Host 

In addition, some other headers are also limited when using the WebClient object. These limited headers include, but are not limited to:

 Accept Connection Content-Length Expect (when the value is set to "100-continue" If-Modified-Since Range Transfer-Encoding 

The HttpWebRequest class has properties for setting some of the above headers. If it is important for applications to set these headers, then the HttpWebRequest class should be used instead of the WebRequest.

I suspect the reason is that many headers, such as date and host, must be set differently with another request. You should not copy them. In fact, I would personally suggest that you should not copy any of them. Put in your own user agent. If the page you get depends on a specific value, then I think you want you to always send a valid value, rather than relying on the original user to provide you this information.

Essentially work out what you need to do, and donโ€™t find something that works and does it, without fully understanding what you are doing.

+5
source

It looks like you are trying to set some header that should be set using one of the WebClient properties ( CachePolicy , ContentLength or ContentType )

Also, it's not good to blindly copy all the headers, you only need to get the ones you really need.

+1
source

Source: https://habr.com/ru/post/898277/


All Articles