HttpWebRequest.Headers Property


指定构成 HTTP 标头的名称/值对的集合。Specifies a collection of the name/value pairs that make up the HTTP headers.

 virtual property System::Net::WebHeaderCollection ^ Headers { System::Net::WebHeaderCollection ^ get(); void set(System::Net::WebHeaderCollection ^ value); };
public override System.Net.WebHeaderCollection Headers { get; set; }
member this.Headers : System.Net.WebHeaderCollection with get, set
Public Overrides Property Headers As WebHeaderCollection

Property Value


包含构成 HTTP 请求标头的名称/值对的 WebHeaderCollectionA WebHeaderCollection that contains the name/value pairs that make up the headers for the HTTP request.



下面的代码示例使用 Headers 属性,将 HTTP 标头名称/值对打印到控制台。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( "" ) );
// 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 );
// Release the HttpWebResponse Resource.
// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("");
// 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);
    count = streamRead.Read(readBuff, 0, 256);
// Close the Stream object.
// Release the HttpWebResponse Resource.
     ' 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.


Headers 集合包含与请求关联的协议标头。The Headers collection contains the protocol headers associated with the request. 下表列出了不存储在 Headers 集合中但由系统设置或由属性或方法设置的 HTTP 标头。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 Accept 属性设置。Set by the Accept property.
连接Connection Connection 属性和 KeepAlive 属性设置。Set by the Connection property and KeepAlive property.
Content-LengthContent-Length ContentLength 属性设置。Set by the ContentLength property.
Content-TypeContent-Type ContentType 属性设置。Set by the ContentType property.
ExpectExpect Expect 属性设置。Set by the Expect property.
日期Date Date 属性设置。Set by the Date property.
HostHost Host 属性设置。Set by the Host property.
If-Modified-SinceIf-Modified-Since IfModifiedSince 属性设置。Set by the IfModifiedSince property.
范围Range AddRange 方法设置。Set by the AddRange method.
RefererReferer Referer 属性设置。Set by the Referer property.
Transfer-EncodingTransfer-Encoding TransferEncoding 属性设置(SendChunked 属性必须为 true)。Set by the TransferEncoding property (the SendChunked property must be true).
User-AgentUser-Agent UserAgent 属性设置。Set by the UserAgent property.

如果尝试设置其中一个受保护的标头,Add 方法将引发 ArgumentExceptionThe Add method throws an ArgumentException if you try to set one of these protected headers.

通过调用 GetRequestStreamBeginGetRequestStreamGetResponseBeginGetResponse 方法启动请求后,更改 Headers 属性会引发 InvalidOperationExceptionChanging the Headers property after the request has been started by calling GetRequestStream, BeginGetRequestStream, GetResponse, or BeginGetResponse method throws an InvalidOperationException.

不应假定标头值保持不变,因为 Web 服务器和缓存可能会更改或向 Web 请求添加标头。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.

Applies to