HttpWebRequest.GetResponse 方法
GetResponse 方法返回包含来自 Internet 资源的响应的 WebResponse 对象。 实际返回的实例是 HttpWebResponse,并且能够转换为访问 HTTP 特定的属性的类。
在一些情况下,当对 HttpWebRequest 类设置的属性发生冲突时将引发 ProtocolViolationException。 如果应用程序将 ContentLength 属性和 SendChunked 属性设置为true,然后发送 HTTP GET 请求,则会引发该异常。 如果应用程序尝试向仅支持 HTTP 1.0 协议而不支持分块请求的服务器发送分块请求,则会引发该异常。 如果应用程序未设置 ContentLength 属性就尝试发送数据,或者在 keepalive 连接(KeepAlive 属性为 true)上禁用缓冲时 SendChunked 为 false,则会引发该异常。
警告 |
---|
必须调用 Close 方法关闭该流并释放连接。 如果未能做到这一点,可能导致应用程序用完连接。 |
使用 POST 方法时,必须获取请求流,写入要发送的数据,然后关闭请求流。 此方法阻塞以等待发送的内容;如果没有超时设置并且您没有提供内容,调用线程将无限期地阻塞。
说明 |
---|
多次调用 GetResponse 会返回相同的响应对象;该请求不会重新发出。 |
说明 |
---|
应用程序不能对特定请求混合使用同步和异步方法。 如果调用 GetRequestStream 方法,则必须使用 GetResponse 方法检索响应。 |
说明 |
---|
如果引发 WebException,请使用该异常的 Response 和 Status 属性确定服务器的响应。 |
说明 |
---|
当应用程序中启用了网络跟踪时,此成员将输出跟踪信息。 有关详细信息,请参阅 网络跟踪。 |
说明 |
---|
为安全起见,默认情况下禁用 Cookie。 如果您希望使用 Cookie,请使用 CookieContainer 属性启用 Cookie。 |
using System;
using System.Net;
using System.Text;
using System.IO; public class Test
{
// Specify the URL to receive the request.
public static void Main (string[] args)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create (args[]); // Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = ;
request.MaximumResponseHeadersLength = ;
// Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse (); Console.WriteLine ("Content length is {0}", response.ContentLength);
Console.WriteLine ("Content type is {0}", response.ContentType); // Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream (); // Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8); Console.WriteLine ("Response stream received.");
Console.WriteLine (readStream.ReadToEnd ());
response.Close ();
readStream.Close ();
}
} /*
The output from this example will vary depending on the value passed into Main
but will be similar to the following: Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.
<html>
...
</html> */
HttpWebRequest.GetResponse 方法的更多相关文章
- HttpWebRequest.GetResponse 方法 转载
GetResponse 方法返回包含来自 Internet 资源的响应的 WebResponse 对象. 实际返回的实例是 HttpWebResponse,并且能够转换为访问 HTTP 特定的属性的类 ...
- HttpWebRequest.GetResponse() raises exception when http status code 400 (bad request) is returned
参考: .Net HttpWebRequest.GetResponse() raises exception when http status code 400 (bad request) is re ...
- System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse()
System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse() 在请求 ...
- System.Net.HttpWebRequest.GetResponse() 远程服务器
WebException 服务器状态码错误,比如500服务器内部错误 现象 我们编码实现请求一个页面时,请求的代码类似如下代码: HttpWebRequest req = (HttpWebReques ...
- 利用HttpWebRequest实现实体对象的上传
一 简介 HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择.它们支持一系列有用的属性.这两个类位 于System.Net命名空间,默认情况下这个类对 ...
- httpwebrequest详解【转】
http://blog.csdn.net/sjj2011/article/details/7823392 HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最 ...
- C#中HttpWebRequest的用法详解
原文链接:http://www.cnblogs.com/love201314/p/5029312.html 1.HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数 ...
- httpwebrequest详解
HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择.它们支持一系列有用的属性.这两个类位 于System.Net命名空间,默认情况下这个类对于控制台程 ...
- C#中HttpWebRequest的用法详解(转载)
1.HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择.2.命名空间:System.Net3.HttpWebRequest对象不是利用new关键字创建 ...
随机推荐
- post、get的区别
get的参数会显示在浏览器地址栏中,而 post的参数不会显示在浏览器地址栏中: 使用 post提交的页面在点击[刷新]按钮的时候浏览器一般会提示“是否重新提交”,而 get则不会: 用get的页面可 ...
- tar命令的详细解释
tar命令的详细解释 标签: linuxfileoutputbashinputshell 2010-05-04 12:11 235881人阅读 评论(12) 收藏 举报 分类: linux/unix ...
- 基本linux命令
1.mkdir mkdir 创建目录 mkdir -p 循环创建目录 2.cd 切换目录 3.pwd 查看当前路径 4.mkdir 删除一个空的目录 5.cp 复制文件/目录 -r用 ...
- log4net的基本配置及用法
[1].[代码] [C#]代码 跳至 [1] [2] ? 1 2 using System.Reflection; //使用反射 static private ILog log = log4net. ...
- 湖南附中模拟day1 瞭望塔
/* 这个题要用到树的性质,一般比较难的图论题会往这方面靠拢,这样用很容易出错,应该先写暴力,然后再去一点点想正解 */ //暴力70分 #include<iostream> #inclu ...
- 基于iSCSI的SQL Server 2012群集测试(四)--模拟群集故障转移
6.模拟群集故障转移 6.1 模拟手动故障转移(1+1) 模拟手动故障转移的目的有以下几点: 测试群集是否能正常故障转移 测试修改端口是否能同步到备节点 测试禁用full-text和Browser服务 ...
- 新手学JavaScript都要学什么?
要从事网页开发,不知从何下手? 会JavaScript语言但是不知道该如何进阶! 最好的方法就是不断地做网页.写代码,不断地看别人的代码,研究别人的代码! 用代码实践JS的每个技巧点:多听多看多问多写 ...
- 2013区域赛长沙赛区现场赛 K - Pocket Cube
K - Pocket Cube Time Limit:10000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- 格式化Double类型
//格式化Double类型 //F:默认是2位小数点 //F6:输出小数点后6位,不够的话用0补齐 //G:默认输出原先的,保留小数点后面的位数 LalTotal.Text = "合计:原始 ...
- Java Io 对象序列化和反序列化
Java 支持将任何对象进行序列化操作,序列化后的对象文件便可通过流进行网络传输. 1. 对象序列化就是将对象转换成字节序列,反之叫对象的反序列化 2. 序列化流ObjectOut ...