A System.Net.WebHeaderCollection containing the name/value pairs of the headers for the current instance.
Type Reason InvalidOperationException A set operation was requested but data has already been written to the request data stream.
The HttpWebRequest.Headers collection contains the protocol headers associated with the request. The following table lists the HTTP headers that are not stored in the HttpWebRequest.Headers collection but are either set by the system or set by properties or methods.
Accept |
Set by the HttpWebRequest.Accept property. |
Connection |
Set by the HttpWebRequest.Connection property and HttpWebRequest.KeepAlive property. |
Content-Length |
Set by the HttpWebRequest.ContentLength property. |
Content-Type |
Set by the HttpWebRequest.ContentType property. |
Expect |
Set by the HttpWebRequest.Expect property. |
Date |
Set by the HttpWebRequest.Date property. |
Host |
Set by the HttpWebRequest.Host property. |
If-Modified-Since |
Set by the HttpWebRequest.IfModifiedSince property. |
Range |
Set by the HttpWebRequest.AddRange(int, int) method. |
Referer |
Set by the HttpWebRequest.Referer property. |
Transfer-Encoding |
Set by the HttpWebRequest.TransferEncoding property (the HttpWebRequest.SendChunked property must be true). |
User-Agent |
Set by the HttpWebRequest.UserAgent property. |
The WebHeaderCollection.Add(HttpRequestHeader, string) method throws an ArgumentException if you try to set one of these protected headers.
Changing the HttpWebRequest.Headers property after the request has been started by calling HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream(AsyncCallback, object), HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse(AsyncCallback, object) method throws an InvalidOperationException.
You should not assume that the header values will remain unchanged, because Web servers and caches may change or add headers to a Web request.