HttpWebRequest.Headers 属性


指定构成 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



包含构成 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
AcceptAccept 由属性设置 AcceptSet by the Accept property.
连接Connection Connection 属性和属性设置 KeepAliveSet by the Connection property and KeepAlive property.
Content-LengthContent-Length 由属性设置 ContentLengthSet by the ContentLength property.
Content-TypeContent-Type 由属性设置 ContentTypeSet by the ContentType property.
ExpectExpect 由属性设置 ExpectSet by the Expect property.
DateDate 由属性设置 DateSet by the Date property.
主机Host 由属性设置 HostSet by the Host property.
If-Modified-SinceIf-Modified-Since 由属性设置 IfModifiedSinceSet by the IfModifiedSince property.
范围Range 由方法设置 AddRangeSet by the AddRange method.
RefererReferer 由属性设置 RefererSet by the Referer property.
Transfer-EncodingTransfer-Encoding 由属性设置 TransferEncoding (SendChunked 属性必须为 true) 。Set by the TransferEncoding property (the SendChunked property must be true).
User-AgentUser-Agent 由属性设置 UserAgentSet by the UserAgent property.

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

Headers通过调用 GetRequestStream 、、或方法启动请求后更改属性会 BeginGetRequestStream GetResponse BeginGetResponse 引发 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.