EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.

WebHeaderCollection.Item Property (String)

May 11, 2014

Gets or sets the specified request header.

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

public string this[
	string name
] { get; set; }

Parameters

name
Type: System.String
The name of the requested header.

Property Value

Type: System.String
A String instance containing the specified header value.

ExceptionCondition
ArgumentException

The name parameter contains illegal characters- or -

an attempt was made to set a restricted header on the WebHeaderCollection object associated with a HttpWebRequest object

ArgumentNullException

The name parameter was null.

InvalidOperationException

On a get or set operation, the request header specified in the name parameter is a restricted header. The WebHeaderCollection class does not allow a user to get or set instances of this restricted header in this WebHeaderCollection instance.

The Item[String] property can be used to get or set the value of a header for use by WebClient or HttpWebRequest classes. The Item[String] property can be used to get the value of custom header or a header not defined in the HttpRequestHeader enumeration for use by the WebClient or HttpWebRequest classes. The Item[String] property can also be used to get the value of a common header defined in the HttpRequestHeader enumeration by passing the string of the common header name as the name parameter.

The Item[String] property can also be used to set the name and value for header. When setting a header, both a name and value parameter must be passed in as strings

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[String] property 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 later when an attempt to send the WebClient request occurs.

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

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Afficher:
© 2014 Microsoft