HttpWebResponse,并且能够转换为访问 HTTP 特定的属性的类。

 

 

警告

如果未能做到这一点,可能导致应用程序用完连接。

此方法阻塞以等待发送的内容;如果没有超时设置并且您没有提供内容,调用线程将无限期地阻塞。

 

说明

GetResponse 会返回相同的响应对象;该请求不会重新发出。

说明

GetResponse 方法检索响应。

 

 

说明

Status 属性确定服务器的响应。

说明

网络跟踪。

说明

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 */

相关文章: