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. JAVA 十六进制与字符串的转换

    public static String toHexString(int i)以十六进制的无符号整数形式返回一个整数参数的字符串表示形式.如果参数为负,那么无符号整数值为参数加上 232:否则等于该参 ...

  2. 使用Maven构建RichFaces 4.x项目

    使用Maven构建RichFaces 4.x项目 目录 开始之前 第一步 - 创建Maven项目 第二布 - 添加依赖文件 第三步 - 配置RichFaces 第四步 - 创建显示页面 开始之前 本文 ...

  3. [强连通分量] POJ 1236 Network of Schools

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16803   Accepted: 66 ...

  4. 【转】 linux下的g++编译器安装

    再debian下直接apt-get install gcc g++就可以了.按照类似的逻辑,再Fedora下yum install gcc g++ 报告无法找到g++包. 查了一下,原来这个包的名字叫 ...

  5. Android M新特性之Permissions

    User does not have to grant any permissions when they install or upgrade the app. Instead, the app r ...

  6. ajaxsearch要点1

    ajaxsearch中必须将form的class定义为pagerForm,才能在foot中submit按钮得到值

  7. zlhome.com Deal

    using AnfleCrawler.Common; using System; using System.Collections.Generic; using System.Linq; using ...

  8. HBase with MapReduce (Only Read)

    最近在学习HBase,在看到了如何使用Mapreduce来操作Hbase,下面将几种情况介绍一下,具体的都可以参照官网上的文档说明.官网文档连接:http://hbase.apache.org/boo ...

  9. Web加载资源问题

    Web加载静态资源的时候是同步加载的,每次加载必须前一个加载完成后进行后一个加载,这个是由于javascript 去阻塞浏览器其它操作导致的 推荐文章:http://www.cnblogs.com/l ...

  10. IT行业的正式入门

    虽然我是计算机专业毕业的大学生,但我自己认为我连什么是 IT都不了解,我热爱Java程序的设计,所以我现在在努力学习,今天是上Java程序设计的第一天,我正式进入IT业,踏上了这条“不归路”.figh ...