HttpWebRequest.Headers 属性
2009-11-24 13:26
411 查看
指定构成 HTTP 标头的名称/值对的集合。
Headers 集合包含与请求关联的协议标头。下表列出了由系统或由属性或方法设置但未存储在 Headers 中的 HTTP 标头。
如果您试图设置这些受保护的标头之一,则 Add 方法将引发 ArgumentException。
在通过调用 GetRequestStream、BeginGetRequestStream、GetResponse 或 BeginGetResponse 方法启动请求之后,更改 Headers 属性将引发 InvalidOperationException。
不应该假设标头值会保持不变,因为 Web 服务器和缓存可能会更改标头或向 Web 请求添加标头。
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 属性设置。 |
在通过调用 GetRequestStream、BeginGetRequestStream、GetResponse 或 BeginGetResponse 方法启动请求之后,更改 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();
相关文章推荐
- 问题:request.Headers;结果:HttpWebRequest.Headers 属性
- HttpWebRequest..::.CookieContainer 属性
- HttpWebRequest.ReadWriteTimeout 属性
- httpwebrequest的referer属性的一点认识
- HttpWebRequest..::.Referer 属性
- HttpWebRequest.Method属性研究
- HttpWebRequest.Method 属性
- 『备忘』HttpWebRequest 在 POST 提交时, 标头(Headers)丢失原因
- HttpWebRequest.Proxy属性
- c# .net core HttpWebRequest 使用Headers[]方法代替UserAgent
- 使用HttpWebRequest需要设置Accept和UserAgent属性
- 反射:修改请求头HttpWebRequest/Webclient Header属性的date值-"此标头必须使用适当的属性进行修改"
- 用HTTPWebrequest的CharacterSet属性时,结果总是空字符
- 使用HttpWebRequest发送自定义POST请求(转帖)
- C# HttpWebRequest 发送SOAP XML
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
- #.net 中的 HttpWebRequest 和 HttpWebResponse 类发送客户端证书
- Url以.(点)结尾,在使用httpwebrequest读取的时候,微软会有一个bug……
- HttpRequest.Filter 属性
- HttpWebRequest 发送请求及开启监听