System.Net.WebRequest类,是通过HTTP协议与Web服务器进行交互的实现。
开发工具:VS2012,控制台应用程序
以下通过实例代码进行讲解,先不使用代理进行GET和POST调用;然后通过指定代理、本地IE代理进行GET和POST调用,循序渐进的学习。
GET和POST调用实现类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace What21 { public class What21WebRequest { /// <summary> /// www.what21.com /// HTTP GET方法调用 /// </summary> /// <param name="Url">调用的URL地址</param> /// <param name="RequestPara">调用传递参数</param> /// <param name="webProxy">代理对象</param> /// <returns>返回调用内容</returns> public static string GetData( string Url, string RequestPara, WebProxy webProxy) { // 请求数据 RequestPara = RequestPara.IndexOf( '?' ) > -1 ? (RequestPara) : ( "?" + RequestPara); // 创建WebRequest调用对象 WebRequest request = HttpWebRequest.Create(Url + RequestPara); if (webProxy!= null ) { // 设置通过代理完成调用 request.Proxy = webProxy; } // 将调用数据转换为字节数组 byte [] buf = Encoding.GetEncoding( "UTF-8" ).GetBytes(RequestPara); // 调用方法为GET request.Method = "GET" ; // 调用返回内容 string ReturnVal = "" ; // 发起调用操作 using (WebResponse response = request.GetResponse()) { // 响应数据流 Stream stream = response.GetResponseStream(); // 以UTF-8编码转换为StreamReader StreamReader reader = new StreamReader(stream, Encoding.GetEncoding( "UTF-8" )); // 读取至结束 ReturnVal = reader.ReadToEnd(); reader.Close(); response.Close(); } return ReturnVal; } /// <summary> /// www.what21.com /// HTTP GET方法调用 /// </summary> /// <param name="Url">调用的URL地址</param> /// <param name="RequestPara">调用传递参数</param> /// <param name="webProxy">代理对象</param> /// <returns>返回调用内容</returns> public static string PostData( string Url, string RequestPara,WebProxy webProxy) { // 创建WebRequest调用对象 WebRequest request = HttpWebRequest.Create(Url); if (webProxy != null ) { // 设置通过代理完成调用 request.Proxy = webProxy; } // 数据编码为键值对 request.ContentType = "application/x-www-form-urlencoded" ; request.Method = "POST" ; // 将调用数据转换为字节数组 byte [] buf = Encoding.GetEncoding( "UTF-8" ).GetBytes(RequestPara); // 设置HTTP头,提交的数据长度 request.ContentLength = buf.Length; // 写入参数内容 Stream requestStream = request.GetRequestStream(); requestStream.Write(buf, 0, buf.Length); requestStream.Flush(); // 调用返回内容 string ReturnVal = "" ; // 发起调用操作 WebResponse response = request.GetResponse(); // 响应数据流 Stream stream = response.GetResponseStream(); // 以UTF-8编码转换为StreamReader StreamReader reader = new StreamReader(stream, Encoding.GetEncoding( "UTF-8" )); // 读取至结束 ReturnVal = reader.ReadToEnd(); reader.Close(); response.Close(); return ReturnVal; } } } |
重点解释:
application/x-www-form-urlencoded:标准的编码格式,把数据被编码为名称->值对。
WebRequest.ContentLength属性,HTTP请求头指定POST提交的数据长度。
执行GET和POST调用:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// 调用参数 string RequestPara = "q=java" ; // GET调用 string ReturnGETTxt = What21WebRequest.GetData(Url, RequestPara, null ); Console.WriteLine( "http get data : {0}" , ReturnGETTxt); // POST调用 string ReturnPOSTTxt = What21WebRequest.PostData(Url, RequestPara, null ); Console.WriteLine( "http post data : {0}" , ReturnPOSTTxt); Console.WriteLine( "what21.com prompt, Press any key to continue . . . " ); Console.ReadKey( true ); |
GET和POST传递参数都相同,如果是多个参数均为:param1=value1¶m2=value2¶m3=value3
使用指定的代理执行GET和POST调用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
// 创建代理对象 WebProxy myProxy = new WebProxy( "MyProxyAddress" , 80); // 对本地地址不使用代理 myProxy.BypassProxyOnLocal = false ; // 调用参数 string RequestPara = "q=java" ; // GET调用 string ReturnGETTxt = What21WebRequest.GetData(Url, RequestPara, myProxy); Console.WriteLine( "http get data : {0}" , ReturnGETTxt); // POST调用 string ReturnPOSTTxt = What21WebRequest.PostData(Url, RequestPara, myProxy); Console.WriteLine( "http post data : {0}" , ReturnPOSTTxt); Console.WriteLine( "what21.com prompt, Press any key to continue . . . " ); Console.ReadKey( true ); |
System.Net.WebProxy类,指定是否使用Web代理来发送HTTP请求。
使用本地IE代理执行GET和POST调用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// 使用IE代理 WebProxy myProxy = WebProxy.GetDefaultProxy(); // 调用参数 string RequestPara = "q=java" ; // GET调用 string ReturnGETTxt = What21WebRequest.GetData(Url, RequestPara, myProxy); Console.WriteLine( "http get data : {0}" , ReturnGETTxt); // POST调用 string ReturnPOSTTxt = What21WebRequest.PostData(Url, RequestPara, myProxy); Console.WriteLine( "http post data : {0}" , ReturnPOSTTxt); Console.WriteLine( "what21.com prompt, Press any key to continue . . . " ); Console.ReadKey( true ); |
WebProxy.GetDefaultProxy()静态方法获取Internet Explorer中已配置好的设置。
调用执行结果均为:
经过检查,以上3个执行:无代理调用、指定代理调用和使用IE代理调用,均为这个结果,GET和POST调用都成功。
from:http://www.what21.com/sys/view/csharp-net_12_1474898299666.html