HttpWebRequest.Headers 属性

定义

指定构成 HTTP 标头的名称/值对的集合。

public:
 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

属性值

WebHeaderCollection

包含构成 HTTP 请求标头的名称/值对的 WebHeaderCollection

例外

示例

下面的代码示例使用 Headers 属性将 HTTP 标头名称/值对打印到控制台。

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

注解

Headers 集合包含与请求关联的协议标头。 下表列出了未存储在集合中的 Headers HTTP 标头,但由系统或属性或方法设置。

标头 设置依据
Accept Accept 属性设置。
连接 Connection 属性和 KeepAlive 属性设置。
Content-Length ContentLength 属性设置。
Content-Type ContentType 属性设置。
Expect Expect 属性设置。
Date Date 属性设置。
主机 Host 属性设置。
If-Modified-Since IfModifiedSince 属性设置。
范围 AddRange 方法设置。
Referer Referer 属性设置。
Transfer-Encoding 属性 TransferEncoding (SendChunked 属性必须为 true) 。
User-Agent UserAgent 属性设置。

如果尝试设置其中一个ArgumentException受保护的标头,该方法Add将引发该方法。

调用GetRequestStreamBeginGetRequestStreamGetResponseBeginGetResponse方法InvalidOperationException引发请求后更改Headers属性。

不应假定标头值保持不变,因为 Web 服务器和缓存可能会更改或向 Web 请求添加标头。

适用于