System.Net.WebClient.Headers Property

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

Syntax

public WebHeaderCollection Headers { get; set; }

Value

A System.Net.WebHeaderCollection containing header name/value pairs associated with the request or, if this property has not been set or was set to null, a new instance of the System.Net.WebHeaderCollection class.

Remarks

The WebClient.Headers property contains a System.Net.WebHeaderCollection instance containing protocol headers that the System.Net.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 System.Net.WebHeaderCollection object. Any attempt to set one of these restricted headers in the System.Net.WebHeaderCollection object associated with a System.Net.WebClient object will throw an exception later when attempting to send the System.Net.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 System.Net.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 System.Net.HttpWebRequest class has properties for setting some of the above headers. If it is important for an application to set these headers, then the System.Net.HttpWebRequest class should be used instead of the System.Net.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.

Requirements

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