Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 Headers Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
HttpWebRequest..::.Headers Property

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

Namespace:  System.Net
Assembly:  System (in System.dll)
Visual Basic (Declaration)
Public Overrides Property Headers As WebHeaderCollection
Visual Basic (Usage)
Dim instance As HttpWebRequest
Dim value As WebHeaderCollection

value = instance.Headers

instance.Headers = value
C#
public override WebHeaderCollection Headers { get; set; }
Visual C++
public:
virtual property WebHeaderCollection^ Headers {
    WebHeaderCollection^ get () override;
    void set (WebHeaderCollection^ value) override;
}
JScript
public override function get Headers () : WebHeaderCollection
public override function set Headers (value : WebHeaderCollection)

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 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 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 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.

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();

Visual 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 = 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();

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Applications need to be able to set the Host Header      jranson   |   Edit   |   Show History
It is currently impossible to programatically modify the 'Host' header, which most engineers of Enterprise-class applications would agree is an oversight that should be corrected, either by removing the Host header protection, or by exposing a method to modify it. Please consider adding this functionality in the next .NET release.

Consider a scenario where multiple servers (host1.example.com, host2.example.com, and host3.example.com) each virtually serve a group of websites (x.example.com, y.example.com, and z.example.com) from behind a load balancing configuration. Then consider an application such as a Health Check service, which may need to specifically check the health of the instance of z.example.com being served specifically from server2.example.com. In a NLB configuration, it is 66% more likely that a generic request made for z.example.com would be serviced by server1 or server2, than server3. Therefore, the Health Checking agent would need to explicitly connect to server3, and provide a host header of z.example.com, in order to check the health of that instance.

Due to the ease in implementing the HttpWebRequest object, it seems like a prime candidate for any agent that needs to make an Http request. However, the inability to provide a custom Host header makes it impossible to effectivly work in scenarios such as the one described above. I understand there is a work-around in that you can provide the host header in the proxy setting, however, that is suboptimal, as it would then inhibit an agent from utilizing a real proxy that may be necessary for web access.

Tags What's this?: Add a tag
Flag as ContentBug
There is a Feedback item on Microsoft Connnect regarding the Host Header issue.      aptitude   |   Edit   |   Show History
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker