您的位置:首页 > 理论基础 > 计算机网络

HttpWebRequest.Headers 属性

2009-11-24 13:26 411 查看
指定构成 HTTP 标头的名称/值对的集合。



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

标头

设置方

Accept

Accept 属性设置。

Connection

Connection 属性和 KeepAlive 属性设置。

Content-Length

ContentLength 属性设置。

Content-Type

ContentType 属性设置。

Expect

Expect 属性设置。

Date

由系统设置为当前日期。

Host

由系统设置为当前主机信息。

If-Modified-Since

IfModifiedSince 属性设置。

Range

AddRange 方法设置。

Referer

Referer 属性设置。

Transfer-Encoding

TransferEncoding 属性设置(SendChunked 属性必须为 true)。

User-Agent

UserAgent 属性设置。

如果您试图设置这些受保护的标头之一,则 Add 方法将引发 ArgumentException

在通过调用 GetRequestStreamBeginGetRequestStreamGetResponseBeginGetResponse 方法启动请求之后,更改 Headers 属性将引发 InvalidOperationException

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









// 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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: