C# HTTP请求 GET&POST

什么是 HTTP ?

  超文本传输协议(HTTP)的设计目的是保证客户端与服务器之间的通信。

  HTTP 的工作方式是客户端与服务器之间的请求-应答协议。

  web 浏览器可能是客户端,而计算机上的网络应用程序也可能作为服务器端。

  举例:客户端(浏览器)向服务器提交 HTTP 请求;服务器向客户端返回响应。响应包含关于请求的状态信息以及可能被请求的内容。

  

  两种最常用的 HTTP 方法是:GET 和 POST。

  • GET - 从指定的资源请求数据。

  • POST - 向指定的资源提交要被处理的数据。

一. GET请求

  参数是通过Url来传输,接在Url后面中间用?分开,不同参数之间用&分开,也可不带参数,直接从服务器拉取数据。

  • GET 请求可被缓存

  • GET 请求保留在浏览器历史记录中

  • GET 请求可被收藏为书签

  • GET 请求不应在处理敏感数据时使用

  • GET 请求有长度限制

  • GET 请求只应当用于取回数据

  
 1             string result = String.Empty;
 2             string url = $"http://localhost:52307/API/CommonAPI/GetTest1?param1={100}&param2={200}";
 3             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
 4             request.Method = "GET";
 5             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 6             Stream stream = response.GetResponseStream();
 7             if (stream != null)
 8             {
 9                 using (StreamReader reader = new StreamReader(stream, Encoding.Default))
10                 {
11                     result = reader.ReadToEnd();
12                     reader.Close();
13                 }
14             }
15 
16             Console.WriteLine($"result={result}");
17             Console.ReadKey(); 
View Code


一. POST请求

  

   请注意,查询字符串(名称/值对)是在 POST 请求的 HTTP 消息主体中发送的

  • POST 请求不会被缓存

  • POST 请求不会保留在浏览器历史记录中

  • POST 不能被收藏为书签

  • POST 请求对数据长度没有要求

  1. ContentType = "application/x-www-form-urlencoded"

  
 1             string result = String.Empty;
 2 
 3             string data = $"param1={100}&param2={200}";
 4             byte[] bytes = Encoding.Default.GetBytes(data);
 5 
 6             string url = $"http://localhost:52307/API/CommonAPI/PostTest2";
 7             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
 8             request.Method = "POST";
 9             request.ContentType = "application/x-www-form-urlencoded";
10             request.ContentLength = bytes.Length;
11             Stream requestStream = request.GetRequestStream();
12             requestStream.Write(bytes, 0, bytes.Length);
13             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
14             Stream responseStream = response.GetResponseStream();
15             if (responseStream != null)
16             {
17                 using (StreamReader reader = new StreamReader(responseStream, Encoding.Default))
18                 {
19                     result = reader.ReadToEnd();
20                     reader.Close();
21                 }
22             }
23 
24             Console.WriteLine($"result={result}");
25             Console.ReadKey(); 
View Code

   WebApi 获取请求参数:

 方法1. 
 1             try
 2             {
 3                 HttpContextBase httpContextBase = (HttpContextBase)Request.Properties["MS_HttpContext"];
 4                 HttpRequestBase httpRequestBase = httpContextBase.Request;
 5 
 6                 string param1 = httpRequestBase.Form["param1"];
 7                 string param2 = httpRequestBase.Form["param2"];
 8                 PrintLog.WriteLog(LogType.DEBUG, "CommonAPIController", "PostTest2", $"param1={param1},param2={param2}");
 9 
10                 return JsonConvert.SerializeObject(new { param1, param2 });
11             }
12             catch (Exception)
13             {
14 
15                 throw;
16             }
View Code
 方法2. 
 1         public string PostTest1([FromBody]AClass p)
 2         {
 3             try
 4             {
 5                 PrintLog.WriteLog(LogType.DEBUG, "CommonAPIController", "PostTest1", $"param1={p.Param1},param2={p.Param2}");
 6 
 7                 return JsonConvert.SerializeObject(new { p.Param1, p.Param2 });
 8             }
 9             catch (Exception)
10             {
11 
12                 throw;
13             }
14         }
15         public class AClass
16         {
17             public string Param1 { get; set; }
18             public string Param2 { get; set; }
19         }
View Code

  2. ContentType = "application/json"

  
 1             string result = String.Empty;
 2 
 3             string data = "{\"param1\":100,\"param2\":200}";
 4             byte[] bytes = Encoding.Default.GetBytes(data);
 5 
 6             string url = $"http://localhost:52307/API/CommonAPI/PostTest1";
 7             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
 8             request.Method = "POST";
 9             request.ContentType = "application/json";
10             request.ContentLength = bytes.Length;
11             Stream requestStream = request.GetRequestStream();
12             requestStream.Write(bytes, 0, bytes.Length);
13             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
14             Stream responseStream = response.GetResponseStream();
15             if (responseStream != null)
16             {
17                 using (StreamReader reader = new StreamReader(responseStream, Encoding.Default))
18                 {
19                     result = reader.ReadToEnd();
20                     reader.Close();
21                 }
22             }
23 
24             Console.WriteLine($"result={result}");
25             Console.ReadKey(); 
View Code

  WebApi 获取请求参数: 

 方法1.  
 1         [HttpPost]
 2         public string PostTest1([FromBody]AClass p)
 3         {
 4             try
 5             {
 6                 PrintLog.WriteLog(LogType.DEBUG, "CommonAPIController", "PostTest1", $"param1={p.Param1},param2={p.Param2}");
 7 
 8                 return JsonConvert.SerializeObject(new { p.Param1, p.Param2 });
 9             }
10             catch (Exception)
11             {
12 
13                 throw;
14             }
15         }
16         public class AClass
17         {
18             public string Param1 { get; set; }
19             public string Param2 { get; set; }
20         }
View Code

猜你喜欢

转载自www.cnblogs.com/lu-yuan/p/12122234.html