Share via


WebClient.Headers Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Property Headers As WebHeaderCollection
public WebHeaderCollection Headers { get; set; }

Property Value

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

Remarks

The Headers property contains a WebHeaderCollection instance containing header information that the WebClient sends with the request. This is an unrestricted collection of headers, so setting headers that are restricted by WebRequest descendants such as HttpWebRequest is allowed.

Some headers are considered restricted and are either exposed directly (such as Content-Type) or protected by the system and cannot be set in a WebHeaderCollection object. Any attempt to set one of these restricted headers in the WebHeaderCollection object using the Item[HttpRequestHeader] or Item[String] property or throws an exception. If the WebHeaderCollection object is associated with a HttpWebRequest object, the exception is thrown by the Item[HttpRequestHeader] property. If the WebHeaderCollection object is associated with a WebClient object, the exception is thrown when an attempt to send the WebClient request occurs.

For a list of restricted headers, see the Remarks in the WebHeaderCollection class.

For security reasons, the Silverlight runtime restricts the WebClient class from sending specific headers to a cross-domain site unless the header is allowed by the security policy applicable to the target cross-domain site. This restriction applies to resources from locations other than the site of origin. The Authorization header can be set using the Headers property. However to set the credentials properly, the cross-domain policy applicable to the target must have the http-request-headers set to allow the Authorization header to be sent.

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.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.