共用方式為


WebHeaderCollection Class

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

Contains protocol headers associated with a request or response.

Inheritance Hierarchy

System.Object
  System.Net.WebHeaderCollection

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

Syntax

'Declaration
<DefaultMemberAttribute("Item")> _
Public NotInheritable Class WebHeaderCollection _
    Implements IEnumerable
[DefaultMemberAttribute("Item")]
public sealed class WebHeaderCollection : IEnumerable

The WebHeaderCollection type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows Phone WebHeaderCollection Initializes a new instance of the WebHeaderCollection class.

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows Phone AllKeys Gets the collection of header name/value pairs associated with the request.
Public propertySupported by Silverlight for Windows Phone Count Gets the number of headers in the collection.
Public propertySupported by Silverlight for Windows Phone Item[HttpRequestHeader] Gets or sets the specified request header.
Public propertySupported by Silverlight for Windows Phone Item[String] Gets or sets the specified request header.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows Phone Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone ToString Returns a string that represents the current object. (Inherited from Object.)

In Silverlight for Windows Phone, this member is overridden by ToString().

In XNA Framework, this member is overridden by ToString().

Top

Extension Methods

  Name Description
Public Extension MethodSupported by Silverlight for Windows Phone AsQueryable Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodSupported by Silverlight for Windows Phone Cast<TResult> Converts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodSupported by Silverlight for Windows Phone OfType<TResult> Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows Phone IEnumerable.GetEnumerator Returns an enumerator that can iterate through the WebHeaderCollection instance.

Top

Remarks

The WebHeaderCollection class is generally accessed through WebRequest.Headers property.

The Item property can be used to get the value of a header stored in a WebHeaderCollection object. The Item[HttpRequestHeader] property can be used to get the value of common headers defined in the HttpRequestHeader enumeration. The Item[String] property can be used to get the value of custom headers and those headers not defined in the HttpRequestHeader enumeration.

The Item property can be used to set custom headers in a WebHeaderCollection object for use by the WebClient or HttpWebRequest objects.

Some common headers are considered restricted and are either exposed directly (such as Content-Type) or 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 HttpWebRequest object throws an exception. 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 include, but are not limited to the following:

  • Accept

  • Accept-Charset

  • Accept-Encoding

  • Accept-Language

  • Accept-Ranges

  • Age

  • Allow

  • Allowed

  • Connect

  • Connection

  • Content-Length

  • Content-Location

  • Content-Range

  • Content-Transfer-Encoding

  • Content-Type

  • Cookie

  • Date

  • Delete

  • ETag

  • Expect

  • Get

  • Head

  • Host

  • Keep-Alive

  • Last-Modified

  • Location

  • Max-Forwards

  • Options

  • Post

  • Proxy-Authenticate

  • Proxy-Authorization

  • Proxy-Connection

  • Public

  • Put

  • Range

  • Referer

  • Request-Range

  • TE

  • Trace

  • Trailer

  • Transfer-Encoding

  • Upgrade

  • User-Agent

  • Via

  • Warning

  • Uri

  • X-Flash-Version

  • Retry-After

  • Server

  • Vary

  • WWW-Authenticate

Many of these restricted headers are set by the web browser that hosts the Silverlight application.

In addition, some headers are also restricted when the Method property is set to an HTTP GET protocol verb. These restricted headers include, but are not limited to the following:

  • Content-Encoding

  • Content-Language

  • Content-Location

  • Content-MD5

  • Content-Range

  • Content-Type

  • Expires

  • Last-Modified

In addition, some headers are also restricted when the Method property is set to an HTTP POST protocol verb. These restricted headers include, but are not limited to the following:

  • Content-Location

  • Content-Range

  • Last-Modified

For security reasons, the Silverlight runtime restricts 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. For more information, see Network Security Access Restrictions in Silverlight and URL Access Restrictions in Silverlight.

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 Mozilla FireFox browser does not currently set the Referer header on HTTP GET requests. The Internet Explorer and Safari web browsers do properly set the Referer header on HTTP GET requests. As a result, a Silverlight application hosted in Firefox can't support the Referer header being set on any GET requests applicable to browser HTTP handling for BrowserHttp. The Referer header is properly set on HTTP POST requests by Firefox, Internet Explorer, and Safari applicable to browser HTTP handling for BrowserHttp.

The Referer header is properly set on all HTTP requests applicable to client HTTP handling for ClientHttp regardless of the web browser.

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.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.