HttpWebRequest::Headers Property
Specifies a collection of the name/value pairs that make up the HTTP headers.
Assembly: System (in System.dll)
public: property WebHeaderCollection^ Headers { virtual WebHeaderCollection^ get() override; virtual void set(WebHeaderCollection^ value) override; }
Property Value
Type: System.Net::WebHeaderCollection^A WebHeaderCollection that contains the name/value pairs that make up the headers for the HTTP request.
| Exception | Condition |
|---|---|
| InvalidOperationException | The request has been started by calling the GetRequestStream, BeginGetRequestStream, GetResponse, or BeginGetResponse method. |
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 Date property. |
Host | Set by the Host property. |
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 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.
The following code example uses the Headers property to print the HTTP header name/value pairs to the console.
// 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 = gcnew StreamReader( streamResponse ); array<Char>^ readBuff = gcnew array<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 = gcnew String( readBuff,0,count ); Console::Write( outputData ); count = streamRead->Read( readBuff, 0, 256 ); } streamResponse->Close(); streamRead->Close(); // Release the HttpWebResponse Resource. myHttpWebResponse->Close();
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1