Export (0) Print
Expand All

HttpWebRequest.Headers Property

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

[Visual Basic]
Overrides Public Property Headers As WebHeaderCollection
[C#]
public override WebHeaderCollection Headers {get; set;}
[C++]
public: __property WebHeaderCollection* get_Headers();
public: __property void set_Headers(WebHeaderCollection*);
[JScript]
public override function get Headers() : WebHeaderCollection;
public override function set Headers(WebHeaderCollection);

Property Value

A WebHeaderCollection containing the name/value pairs that make up the headers for the HTTP request.

Exceptions

Exception Type Condition
InvalidOperationException The request has been started by calling the GetRequestStream, BeginGetRequestStream, GetResponse, 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 Accept property.
Connection Set by the Connection property and KeepAlive property.
Content-Length Set by the ContentLength property.
Content-Type Set by the ContentType property.
Expect Set by the Expect property.
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 IfModifiedSince property.
Range Set by the AddRange method.
Referer Set by the Referer property.
Transfer-Encoding Set by the TransferEncoding property (the SendChunked property must be true).
User-Agent Set by the UserAgent property.

The Add method throws an ArgumentException if you try to set one of these protected headers.

Changing the Headers property after the request has been started by calling GetRequestStream, BeginGetRequestStream, GetResponse, or BeginGetResponse method will throw an InvalidOperationException.

Example

[Visual Basic, C#, C++] The following example uses the Headers property to print the HTTP header name/value pairs to the console.

[Visual Basic] 
' Create a new 'HttpWebRequest' Object to the mentioned URL.
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.contoso.com"), HttpWebRequest)
' Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
Console.WriteLine(ControlChars.Cr + "The HttpHeaders are " + ControlChars.Cr + ControlChars.Cr + ControlChars.Tab + "Name" + ControlChars.Tab + ControlChars.Tab + "Value" + ControlChars.Cr + "{0}", myHttpWebRequest.Headers)

' Print the HTML contents of the page to the console. 
Dim streamResponse As Stream = myHttpWebResponse.GetResponseStream()
Dim streamRead As New StreamReader(streamResponse)
Dim readBuff(256) As [Char]
Dim count As Integer = streamRead.Read(readBuff, 0, 256)
Console.WriteLine(ControlChars.Cr + "The HTML contents of page the are  : " + ControlChars.Cr + ControlChars.Cr + " ")
While count > 0
    Dim outputData As New [String](readBuff, 0, count)
    Console.Write(outputData)
    count = streamRead.Read(readBuff, 0, 256)
End While
       ' Close the Stream object.
       streamResponse.Close()
       streamRead.Close()
       ' Release the HttpWebResponse Resource.
        myHttpWebResponse.Close()

[C#] 
// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\n{0}",myHttpWebRequest.Headers);
// Print the HTML contents of the page to the console. 
Stream streamResponse=myHttpWebResponse.GetResponseStream();
StreamReader streamRead = new StreamReader( streamResponse );
Char[] readBuff = new Char[256];
int count = streamRead.Read( readBuff, 0, 256 );
Console.WriteLine("\nThe HTML contents of page the are  : \n\n ");    
while (count > 0) 
{
    String outputData = new String(readBuff, 0, count);
    Console.Write(outputData);
    count = streamRead.Read(readBuff, 0, 256);
}
// Close the Stream object.
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse Resource.
myHttpWebResponse.Close();

[C++] 
// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest* myHttpWebRequest =
   dynamic_cast<HttpWebRequest*>(WebRequest::Create(S"http://www.contoso.com"));
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse* myHttpWebResponse =
   dynamic_cast<HttpWebResponse*>(myHttpWebRequest->GetResponse());
Console::WriteLine(S"\nThe HttpHeaders are \n\n\tName\t\tValue\n {0}",
   myHttpWebRequest->Headers);
// Print the HTML contents of the page to the console.
Stream* streamResponse = myHttpWebResponse->GetResponseStream();
StreamReader* streamRead = new StreamReader(streamResponse);
Char readBuff[] = new Char[256];
int count = streamRead->Read(readBuff, 0, 256);
Console::WriteLine(S"\nThe HTML contents of page the are  : \n\n ");
while (count > 0) {
   String* outputData = new String(readBuff, 0, count);
   Console::Write(outputData);
   count = streamRead->Read(readBuff, 0, 256);
}
// Close the Stream object.
streamResponse->Close();
streamRead->Close();
// Release the HttpWebResponse Resource.
myHttpWebResponse->Close();

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

HttpWebRequest Class | HttpWebRequest Members | System.Net Namespace

Show:
© 2014 Microsoft