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。

 1 using System;
2 using System.Net;
3 using System.Text;
4 using System.IO;
5
6
7 public class Test
8 {
9 // Specify the URL to receive the request.
10 public static void Main (string[] args)
11 {
12 HttpWebRequest request = (HttpWebRequest)WebRequest.Create (args[0]);
13
14 // Set some reasonable limits on resources used by this request
15 request.MaximumAutomaticRedirections = 4;
16 request.MaximumResponseHeadersLength = 4;
17 // Set credentials to use for this request.
18 request.Credentials = CredentialCache.DefaultCredentials;
19 HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
20
21 Console.WriteLine ("Content length is {0}", response.ContentLength);
22 Console.WriteLine ("Content type is {0}", response.ContentType);
23
24 // Get the stream associated with the response.
25 Stream receiveStream = response.GetResponseStream ();
26
27 // Pipes the stream to a higher level stream reader with the required encoding format.
28 StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);
29
30 Console.WriteLine ("Response stream received.");
31 Console.WriteLine (readStream.ReadToEnd ());
32 response.Close ();
33 readStream.Close ();
34 }
35 }
36
37 /*
38 The output from this example will vary depending on the value passed into Main
39 but will be similar to the following:
40
41 Content length is 1542
42 Content type is text/html; charset=utf-8
43 Response stream received.
44 <html>
45 ...
46 </html>
47
48 */

HttpWebRequest.GetResponse 方法 转载的更多相关文章

  1. HttpWebRequest.GetResponse 方法

    GetResponse 方法返回包含来自 Internet 资源的响应的 WebResponse 对象. 实际返回的实例是 HttpWebResponse,并且能够转换为访问 HTTP 特定的属性的类 ...

  2. C# 3.0 扩展方法[转载]

    实践 扩展方法是C# 3.0中新加入的特性.MSDN中对扩展方法的定义是:扩展方法使您能够向现有类型"添加"方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 以下以 ...

  3. 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 ...

  4. “error LNK1169: 找到一个或多个多重定义的符号”的解决方法(转载)

    解决方案: “error LNK1169: 找到一个或多个多重定义的符号”的解决方法(转载) 遇到的问题: 在.h头文件中采用namespace 命名空间报错 test.h namespace LMR ...

  5. 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() 在请求 ...

  6. Arcengine 实现要素选取的方法(转载)

    转自原文Arcengine 实现要素选取的方法(转载) 选择一个要素或者一个要素集(FeatureSelection)的方法很多,如IMap::SelectByShape.ILayer::search ...

  7. Cstring转char、string、int等数据类型的方法(转载)

    Cstring转char.string.int等数据类型的方法 (-- ::) 转载 标签: 杂谈 分类: VC CString 转char * CString cstr; char *p = (LP ...

  8. Duilib改进窗口拖动,使整个窗口都能拖动两种方法(转载)

    转载:http://www.cnblogs.com/XiHua/articles/3490490.html 转载:http://blog.csdn.net/lostspeed/article/deta ...

  9. python字符串内容替换的方法(转载)

    python字符串内容替换的方法 时间:2016-03-10 06:30:46来源:网络 导读:python字符串内容替换的方法,包括单个字符替换,使用re正则匹配进行字符串模式查找与替换的方法.   ...

随机推荐

  1. 变量声明提升 Vs. 函数声明提升

    1. 变量声明提升 先看以下代码: 1)var in_window = "a" in window; console.log(in_window); 2)var in_window ...

  2. php函数的可变参数

    <?php function add() { $arr = func_get_args(); //func_num_args() $sum =0; for($i=0;$i<count($a ...

  3. php变量函数,回调函数

    一,变量可以直接传递函数 <?php function demo($num , $n )//$n是个函数 { for($i=0;$i<$num;++$i) { if($n($i)) { e ...

  4. C语言程序设计第一次作业

    同学们,我们已经留了两次实验了,请大家将这两次的实验课内容写成实验报告在截止日期前进行提交. 截止日期:2016-10-7 23:00 实验一: 编程打印5行的倒三角形,第一行打印9个*,第二行7个* ...

  5. 如何在Eclipse中设置默认的JSP文件头部编码

    如何在Eclipse中设置默认的JSP文件头部编码 一般,我们为了以后在导入和导出程序的时候(特别是项目较大,文件多)一般都默认文件编码格式为UTF-8 如果你通常都是通过Eclipse来编写程序,那 ...

  6. C++异常处理的问题

    一般在C语言中,是通过返回值或者设置errno的方式来标识错误的 但在C++里面,构造函数是没有返回值的,于是发明了异常的方式:为了正确的向使用者表明 异常抛出的原因,你必须弄清楚异常抛出的原因(比如 ...

  7. 【RobotFramework自动化测试】RFS常用脚本

    读取后台数据文件:Import Variables | ${CURDIR}/\ABC.py 定位页面:Wait Until Keyword Succeeds | 5s | 500ms | select ...

  8. 简便删除已经存在的oracle数据库用户UPAY3LINGXI_YS

    简便删除已经存在的oracle数据库用户UPAY3LINGXI_YS:1.Toad工具用oracle最大权限用户登录system2.查看正在使用UPAY3LINGXI_YS的进程select * fr ...

  9. Python 基礎 - pyc 是什麼

    Python2.7 版中,只要執行 .py 的檔案後,即會馬上產生一個 .pyc 的檔案,而在 Python3 版中,執行 .py 的檔案後,即會產生一個叫 __pycache__ 的目錄,裡面也會有 ...

  10. c语言scanf返回值

    1. scanf 函数是有返回值的,它的返回值可以分成三种情况 1) 正整数,表示正确输入参数的个数.例如执行 scanf("%d %d", &a, &b); 如果 ...