Web运行原理简单地说是“浏览器发送一个HTTP Request到Web服务器上,Web服务器处理完后将结果(HTTP Response)返回给浏览器”。

通常测试一个web api是否正确,可以通过自己coding方式模拟向Web服务器发送Http Request(设置好各参数),然后再通过捕获Web服务器返回的Http Response并将其进行解析,验证它是否与预期的结果一致。

.net中提供的Http相关的类                                                                              

主要是在命名空间System.Net里,提供了以下几种类处理方式:

WebClient

WebRequest WebResponse

HttpWebRequest HttpWebResponse

TcpClient

Socket

(1)使用WebRequest 获取指定URL的内容测试code

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.IO;
 7 using System.Net;
 8 
 9 namespace Testing01
10 {
11     class WebRequestTest
12     {
13         static void Main(string[] args)
14         {
15             string url = @"http://www.baidu.com";
16             //string html = TestWebClient(url);
17             //string html = TestWebRequest(url);            
18             string html = TestHttpWebRequest(url);            
19             Console.WriteLine(html);
20             Console.Read();
21         }
22 
23         /// <summary>
24         /// 使用WebClient发出http request
25         /// </summary>
26         /// <param name="url"></param>
27         /// <returns></returns>
28         public static string TestWebClient(string url)
29         {
30             Console.WriteLine("Testing WebClient......");
31             WebClient wc = new WebClient();            
32             Stream stream = wc.OpenRead(url);
33             string result = "";
34             using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
35             {
36                 result = sr.ReadToEnd();
37             }
38             return result;
39 
40         }
41 
42         /// <summary>
43         /// 使用WebClient发出http request
44         /// </summary>
45         /// <param name="url"></param>
46         /// <returns></returns>
47         public static string TestWebRequest(string url)
48         {
49             Console.WriteLine("Testing WebRequest......");
50             WebRequest wr = WebRequest.Create(url);
51             wr.Method = "GET";
52             WebResponse response = wr.GetResponse();
53             Stream stream = response.GetResponseStream();
54             string result = "";
55             using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
56             {
57                 result = sr.ReadToEnd();
58             }
59             return result;
60         }
61 
62         /// <summary>
63         /// 使用HttpWebClient发出http request
64         /// </summary>
65         /// <param name="url"></param>
66         /// <returns></returns>
67         public static string TestHttpWebRequest(string url)
68         {
69             Console.WriteLine("Testing HttpWebRequest......");
70             HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
71             wr.Method = "GET";
72             HttpWebResponse response = (HttpWebResponse)wr.GetResponse();
73             Stream stream = response.GetResponseStream();
74             string result = "";
75             using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
76             {
77                 result = sr.ReadToEnd();
78             }
79             return result;
80         }
81     }
82 }
View Code

相关文章:

  • 2022-02-08
  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
  • 2021-05-21
  • 2021-06-23
  • 2022-02-08
  • 2021-11-29
猜你喜欢
  • 2021-08-15
  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
  • 2022-02-06
  • 2022-12-23
  • 2021-11-30
相关资源
相似解决方案