Windows Dev Center

HttpWebRequest.Headers Property

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

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

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.

ExceptionCondition
InvalidOperationException

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

The Headers collection contains the protocol headers associated with the request.

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.

For security reasons, the HttpWebRequest class is restricted 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.


try
{      

  System.Uri uri = new Uri("http://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";
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2015 Microsoft