Export (0) Print
Expand All

WebClient.Headers Property

Gets or sets a collection of header name/value pairs associated with the request.

Namespace:  System.Net
Assembly:  System (in System.dll)

member Headers : WebHeaderCollection with get, set

Property Value

Type: System.Net.WebHeaderCollection
A WebHeaderCollection containing header name/value pairs associated with this request.

The Headers property contains a WebHeaderCollection instance containing protocol headers that the WebClient sends with the request.

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

Restricted headers protected by the system include, but are not limited to the following:

  • Date

  • Host

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

  • 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 an application to set these headers, then the HttpWebRequest class should be used instead of the WebRequest class.

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 collection to set the HTTP Content-Type header to application/x-www-form-urlencoded, to notify the server that form data is attached to the post.

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft