Freigeben über


HttpWebRequest.Headers Property

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

Specifies a collection of the name/value pairs that make up the HTTP headers.

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

Syntax

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

Property Value

Type: System.Net.WebHeaderCollection
A WebHeaderCollection that contains the name/value pairs that make up the headers for the HTTP request.

Exceptions

Exception Condition
InvalidOperationException

The request has been started by calling the BeginGetRequestStream or BeginGetResponse method.

Remarks

The Headers collection contains the protocol headers associated with the request. The following table lists the HTTP headers that are not stored in the Headers collection but are either set by the system or set by properties or methods.

Header

Set by

Accept

Set by the web browser that hosts the Silverlight application.

Connection

Set by the web browser that hosts the Silverlight application.

Content-Length

On Silverlight 4, set by the ContentLength property. On the browser HTTP stack, this ContentLength property is not required to be set because the Silverlight runtime will automatically populate the header with the correct value based on the buffered request body. If a Silverlight application on either the browser or client HTTP stack sets this property to a value that does not match the size of the provided request body, a ProtocolViolationException is thrown when the request is sent.

On Silverlight 3, set by the system or the web browser that hosts the Silverlight application.

Content-Type

Set by the ContentType property.

This header cannot be set on a GET method. If this header is set, a ProtocolViolationException is thrown.

Expect

Set by the web browser that hosts the Silverlight application.

Date

Set by the system to the current date.

Host

Set by the system to the current host information.

If-Modified-Since

Set by the web browser that hosts the Silverlight application.

Range

Set by the web browser that hosts the Silverlight application.

Referer

Set by the web browser that hosts the Silverlight application.

Transfer-Encoding

Set by the web browser that hosts the Silverlight application.

User-Agent

Set by the web browser that hosts the Silverlight application.

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 throws an exception. The exception may be different based on whether the HTTP GET or POST protocol verb was specified in the Method property. 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.

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.

For security reasons, the Silverlight runtime restricts the HttpWebRequest 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.

Changing the Headers property after the request has been started by calling BeginGetRequestStream or BeginGetResponse method throws an InvalidOperationException.

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. In some cases, a header value may be removed.

Examples

try
{      

  System.Uri uri = new Uri("https://www.contoso.com");
  // Create a HttpWebrequest object to the desired URL.
  HttpWebRequest myHttpWebRequest1= (HttpWebRequest)WebRequest.Create(uri);

  // Create an instance of the RequestState and assign the previous myHttpWebRequest1
  // object to it's request field.  
  RequestState myRequestState = new RequestState();  
  myRequestState.request = myHttpWebRequest1;


  // Start the asynchronous request.
  IAsyncResult result=
    (IAsyncResult) myHttpWebRequest1.BeginGetResponse(new AsyncCallback(RespCallback),myRequestState);

  allDone.WaitOne();

  outputBlock.Text += "The HttpWebRequest.Headers are\n";
  outputBlock.Text += "\tName\t\tValue\n";
  outputBlock.Text += myHttpWebRequest1.Headers;

  // Release the HttpWebResponse resource.
  myRequestState.response.Close();
}
catch(WebException e)
{
  outputBlock.Text += "\nException raised!\n";
  outputBlock.Text += "Message: ";
  outputBlock.Text += e.Message;
  outputBlock.Text += "\nStatus: ";
  outputBlock.Text += e.Status;
  outputBlock.Text += "\n";
}
catch(Exception e)
{
  outputBlock.Text += "\nException raised!\n";
  outputBlock.Text += "\nMessage: ";
  outputBlock.Text += e.Message;
  outputBlock.Text += "\n";
}

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.