System.Net.HttpWebRequest.Headers Property

Specifies a collection of the name/value pairs that make up the HTTP headers.

Syntax

public override WebHeaderCollection Headers { get; set; }

Value

A System.Net.WebHeaderCollection containing the name/value pairs of the headers for the current instance.

Exceptions

TypeReason
InvalidOperationExceptionA set operation was requested but data has already been written to the request data stream.

Remarks

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.

Requirements

Namespace: System.Net
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0