HttpWebRequest.Headers 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定构成 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 请求标头的名称/值对。
例外
请求是通过调用 GetRequestStream()、BeginGetRequestStream(AsyncCallback, Object)、GetResponse()或 BeginGetResponse(AsyncCallback, Object) 方法启动的。
示例
下面的代码示例使用 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()
注解
谨慎
WebRequest
、HttpWebRequest
、ServicePoint
和 WebClient
已过时,不应将其用于新开发。 请改用 HttpClient。
Headers 集合包含与请求关联的协议标头。 下表列出了不存储在 Headers 集合中的 HTTP 标头,但由系统设置,也可以由属性或方法设置。
页眉 | 设置者 |
---|---|
接受 | 由 Accept 属性设置。 |
连接 | 由 Connection 属性和 KeepAlive 属性设置。 |
Content-Length | 由 ContentLength 属性设置。 |
Content-Type | 由 ContentType 属性设置。 |
期望 | 由 Expect 属性设置。 |
日期 | 由 Date 属性设置。 |
主机 | 由 Host 属性设置。 |
If-Modified-Since | 由 IfModifiedSince 属性设置。 |
范围 | 由 AddRange 方法设置。 |
引用者 | 由 Referer 属性设置。 |
Transfer-Encoding | 由 TransferEncoding 属性设置(SendChunked 属性必须为 true)。 |
User-Agent | 由 UserAgent 属性设置。 |
如果尝试设置其中一个受保护的标头,Add 方法将引发 ArgumentException。
通过调用 GetRequestStream、BeginGetRequestStream、GetResponse或 BeginGetResponse 方法启动请求后更改 Headers 属性将引发 InvalidOperationException。
不应假定标头值将保持不变,因为 Web 服务器和缓存可能会更改或向 Web 请求添加标头。