This documentation is archived and is not being maintained.

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)

Public Overrides Property Headers As WebHeaderCollection
Dim instance As HttpWebRequest
Dim value As WebHeaderCollection

value = instance.Headers

instance.Headers = value
/** @property */
public WebHeaderCollection get_Headers ()

/** @property */
public void set_Headers (WebHeaderCollection value)

public override function get Headers () : WebHeaderCollection

public override function set Headers (value : WebHeaderCollection)

Not applicable.

Property Value

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

Exception typeCondition


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.


Set by


Set by the Accept property.


Set by the Connection property and KeepAlive property.


Set by the ContentLength property.


Set by the ContentType property.


Set by the Expect property.


Set by the system to the current date.


Set by the system to the current host information.


Set by the IfModifiedSince property.


Set by the AddRange method.


Set by the Referer property.


Set by the TransferEncoding property (the SendChunked property must be true).


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.
     Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(""), 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)
         count = streamRead.Read(readBuff, 0, 256)
     End While
' Close the Stream object.
' Release the HttpWebResponse Resource.

// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)

// Assign the response object of 'HttpWebRequest' to a 
//'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)
Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\n{0}",
// 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);
    count = streamRead.Read(readBuff, 0, 256);

// Close the Stream object.

// Release the HttpWebResponse Resource.

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0