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. HTML5本地数据库(WebSQL)[转]

    除了sessionStorage和localStorage外,HTML5还支持通过本地数据库进行本地数据存储,HTML5采用的是"SQLite"这种文件型数据库,该数据库多集中在嵌 ...

  2. (BFS)poj2935-Basic Wall Maze

    题目地址 题目与最基本的BFS迷宫的区别就是有一些障碍,可以通过建立三维数组,标记某个地方有障碍不能走.另一个点是输出路径,对此建立结构体时要建立一个pre变量,指向前一个的下标.这样回溯(方法十分经 ...

  3. easyui 之ComboTree 用法Demo

    实现效果如下: HTML部分: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="ser ...

  4. 黑马程序员——OC语言 类和对象

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)类 1)类的声明 代码编写 ①定义一个Car类,拥有2个属性:轮子数 ...

  5. jQuery 实现菜单

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. HttpURLConnection发送和接受返回值

    URL url = new URL(sb.toString());//请求的地址 HttpURLConnection connection = (HttpURLConnection) url.open ...

  7. log4j.properties配置详解(转)

    本篇文章转自http://it.oyksoft.com/log4j/ 非常感谢原创作者的辛勤编写与分享. 一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders ...

  8. iOS运行时与method swizzling

    C语言是静态语言,它的工作方式是通过函数调用,这样在编译时我们就已经确定程序如何运行的.而Objective-C是动态语言,它并非通过调用类的方 法来执行功能,而是给对象发送消息,对象在接收到消息之后 ...

  9. xpcall 安全调用

    -- xpall (调用函数f, 错误函数fe[, 参数]) function fun(a,b)   -- 这里的参数没什么实际作用,就是展示下用法    return a / bend -- xpc ...

  10. 纯CSS3实现轮播切换效果

    使用纯css3实现与轮播器一样的功能. HTML代码: <div class="slide-container"> <input type="radio ...