Export (0) Print
Expand All

HttpWebRequest.Headers Property

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

Namespace:  System.Net
Assemblies:   System (in System.dll)
  System.Net.Requests (in System.Net.Requests.dll)

abstract Headers : WebHeaderCollection with get, set 
override Headers : WebHeaderCollection with get, set

Property Value

Type: System.Net.WebHeaderCollection
A WebHeaderCollection that contains the name/value pairs that make up the headers for the HTTP request.

ExceptionCondition
InvalidOperationException

The request has been started by calling the GetRequestStream, BeginGetRequestStream, GetResponse, or BeginGetResponse method.

The Headers collection contains the protocol headers associated with the request. The following table lists the HTTP headers that are not stored in the Headers collection but are either set by the system or set by properties or methods.

Header

Set by

Accept

Set by the Accept property.

Connection

Set by the Connection property and KeepAlive property.

Content-Length

Set by the ContentLength property.

Content-Type

Set by the ContentType property.

Expect

Set by the Expect property.

Date

Set by the Date property.

Host

Set by the Host property.

If-Modified-Since

Set by the IfModifiedSince property.

Range

Set by the AddRange method.

Referer

Set by the Referer property.

Transfer-Encoding

Set by the TransferEncoding property (the SendChunked property must be true).

User-Agent

Set by the UserAgent property.

The Add method throws an ArgumentException if you try to set one of these protected headers.

Changing the Headers property after the request has been started by calling GetRequestStream, BeginGetRequestStream, GetResponse, or BeginGetResponse 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.

The following code example uses the Headers property to print the HTTP header name/value pairs to the console.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft