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.
source share