问题:request.Headers;结果:HttpWebRequest.Headers 属性
2015-08-21 09:11
645 查看
指定构成 HTTP 标头的名称/值对的集合。
Headers 集合包含与请求关联的协议标头。下表列出了由系统或由属性或方法设置但未存储在 Headers 中的 HTTP 标头。
如果您试图设置这些受保护的标头之一,则 Add 方法将引发 ArgumentException。
在通过调用 GetRequestStream、BeginGetRequestStream、GetResponse 或 BeginGetResponse 方法启动请求之后,更改 Headers 属性将引发 InvalidOperationException。
不应该假设标头值会保持不变,因为 Web 服务器和缓存可能会更改标头或向 Web 请求添加标头。
[c-sharp] view plaincopyprint?
// 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();
版权声明:本文为博主原创文章,未经博主允许不得转载。
HttpWebRequest post 传参
场景是 winform程序通过HttpWebRequest 调用web Api 接口
其中参数没传过去,有做过这东西的,给点建议。
下面贴代码;
C# code?
下面是调用代码
C# code?
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 请求添加标头。
[c-sharp] view plaincopyprint?
// 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();
版权声明:本文为博主原创文章,未经博主允许不得转载。
HttpWebRequest post 传参
场景是 winform程序通过HttpWebRequest 调用web Api 接口
其中参数没传过去,有做过这东西的,给点建议。
下面贴代码;
C# code?
C# code?
相关文章推荐
- MFC笔记:TCP Socket通信
- http statusCode(状态码) 200、300、400、500序列
- Win10系统无线网络连接上了但电脑用不了网怎么解决?
- centos7的网络配置以及设置主机名和绑定IP的问题
- centos7的网络配置以及设置主机名和绑定IP的问题
- linux学习笔记4:linux的任务调度,进程管理,mysql的安装和使用,ssh工具的使用,linux网络编程
- linux学习笔记3:linux的网络配置,rpm包,shell以及samba服务器的使用和安装
- 网络语音视频技术浅议(附多个demo源码下载)
- 【网络流量-二部图最大匹配】poj3041Asteroids
- Xcode 7 App Transport Security has blocked a cleartext HTTP 报错解决办法
- C-BlogServer博客-可并发的http服务器
- 重写IHttpHandler,实现前后端分离
- iOS网络监控— BMReachability
- TCP/IP协议学习(三) STM32中ETH驱动配置注意事项
- SQL Server 建立连接时出现与网络相关的或特定于实例的错误
- No mapping found for HTTP request with URI
- Expected MultipartHttpServletRequest: is a MultipartResolver configured?
- IP地址理解_IP地址=网络地址+主机地址,但是具体前面多少是网络地址看题目说明
- OKHTTP解析json数据
- zoj 2314 Reactor Cooling 有上下界的网络最大流