码迷,mamicode.com
首页 > Web开发 > 详细

HttpWebRequest.GetResponse 方法

时间:2014-08-03 20:27:15      阅读:742      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   os   strong   io   

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 方法,布布扣,bubuko.com

HttpWebRequest.GetResponse 方法

标签:style   blog   http   color   使用   os   strong   io   

原文地址:http://www.cnblogs.com/Sabre/p/3888652.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!