原文地址: https://blog.csdn.net/Sayesan/article/details/78794081?spm=1035.2023.3001.6557&utm_medium=distribute.pc_relevant_bbs_down.none-task-blog-2~default~OPENSEARCH~default-3.nonecase&depth_1-utm_source=distribute.pc_relevant_bbs_down.none-task-blog-2~default~OPENSEARCH~default-3.nonecase
C#客户端发送Http请求与服务端通信
环境介绍
1.软件 vs2013
2. 编程语言c# winform
3. 服务端采用java+spring,restful风格
在客户端,通过HttpWebRequest方法,向服务端发送get,post,put和delete请求,但是由于服务端的接收参数不同,以及在具体请求下有稍微的不同,故分为以下几种情况(本文所有代码均为底层接口的形式给出,可以直接被上层具体方法调用)
1.GET请求,服务端接收参数方式为@RequestParam
get请求接收参数的方式通常均为@RequestParam,此时,请求参数实际上是以 “url?param1=xx¶m2=xx”的形式传递的,所以代码如下
-
-
-
public string GET(string url,Dictionary<String,String> param)
-
-
-
-
-
foreach (var item in param)
-
-
url = url + item.Key + "=" + item.Value+"&";
-
-
url = url.Substring(0, url.Length - 1);
-
-
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
-
-
-
-
-
res = (HttpWebResponse)request.GetResponse();
-
-
-
-
res = (HttpWebResponse)ex.Response;
-
-
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
-
string content = sr.ReadToEnd();
-
-
- 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
2.POST(PUT)请求,服务端接收参数方式为@RequestParam
当在post或者put请求以@RequestParam接收参数时,实际提交参数的形式是类似于表单提交,这种情况下,每一个提交的参数前都需要添加boundary,用于将不同的参数分开,具体的提交方式可以参考我的另一篇文章:C#上传文件到服务端(http://blog.csdn.net/pinebud55/article/details/52182217)
3.POST(PUT)请求,服务端接收参数方式为@RequestBody
对于post请求,除非上传文件的特殊情况,否则我们推荐服务端以requestbody形式接收参数,因为在需要接受的参数较多时,代码可以更加简洁,并且不再需要后期增加接收的参数,body即包含了一个对象所有的属性。
言归正传,这种情况的处理是最理想的,只需要将需要提交的参数以json的方式提交即可,代码如下:
-
-
-
public string POST(string url, Dictionary<String, String> param)
-
-
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
-
CookieContainer cookieContainer = new CookieContainer();
-
request.CookieContainer = cookieContainer;
-
request.AllowAutoRedirect = true;
-
-
request.MaximumResponseHeadersLength = 1024;
-
-
request.AllowAutoRedirect = true;
-
request.MaximumResponseHeadersLength = 1024;
-
request.ContentType = "application/json";
-
JObject json = new JObject();
-
-
-
foreach(var item in param)
-
-
json.Add(item.Key, item.Value);
-
-
-
string jsonstring = json.ToString();
-
byte[] jsonbyte = Encoding.UTF8.GetBytes(jsonstring);
-
Stream postStream = request.GetRequestStream();
-
postStream.Write(jsonbyte, 0, jsonbyte.Length);
-
-
-
-
-
-
res = (HttpWebResponse)request.GetResponse();
-
-
-
-
res = (HttpWebResponse)ex.Response;
-
-
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
-
string content = sr.ReadToEnd();
-
-
C#客户端发送Http请求与服务端通信
环境介绍
1.软件 vs2013
2. 编程语言c# winform
3. 服务端采用java+spring,restful风格
在客户端,通过HttpWebRequest方法,向服务端发送get,post,put和delete请求,但是由于服务端的接收参数不同,以及在具体请求下有稍微的不同,故分为以下几种情况(本文所有代码均为底层接口的形式给出,可以直接被上层具体方法调用)
1.GET请求,服务端接收参数方式为@RequestParam
get请求接收参数的方式通常均为@RequestParam,此时,请求参数实际上是以 “url?param1=xx¶m2=xx”的形式传递的,所以代码如下
-
-
-
public string GET(string url,Dictionary<String,String> param)
-
-
-
-
-
foreach (var item in param)
-
-
url = url + item.Key + "=" + item.Value+"&";
-
-
url = url.Substring(0, url.Length - 1);
-
-
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
-
-
-
-
-
res = (HttpWebResponse)request.GetResponse();
-
-
-
-
res = (HttpWebResponse)ex.Response;
-
-
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
-
string content = sr.ReadToEnd();
-
-
- 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
2.POST(PUT)请求,服务端接收参数方式为@RequestParam
当在post或者put请求以@RequestParam接收参数时,实际提交参数的形式是类似于表单提交,这种情况下,每一个提交的参数前都需要添加boundary,用于将不同的参数分开,具体的提交方式可以参考我的另一篇文章:C#上传文件到服务端(http://blog.csdn.net/pinebud55/article/details/52182217)
3.POST(PUT)请求,服务端接收参数方式为@RequestBody
对于post请求,除非上传文件的特殊情况,否则我们推荐服务端以requestbody形式接收参数,因为在需要接受的参数较多时,代码可以更加简洁,并且不再需要后期增加接收的参数,body即包含了一个对象所有的属性。
言归正传,这种情况的处理是最理想的,只需要将需要提交的参数以json的方式提交即可,代码如下:
-
-
-
public string POST(string url, Dictionary<String, String> param)
-
-
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
-
CookieContainer cookieContainer = new CookieContainer();
-
request.CookieContainer = cookieContainer;
-
request.AllowAutoRedirect = true;
-
-
request.MaximumResponseHeadersLength = 1024;
-
-
request.AllowAutoRedirect = true;
-
request.MaximumResponseHeadersLength = 1024;
-
request.ContentType = "application/json";
-
JObject json = new JObject();
-
-
-
foreach(var item in param)
-
-
json.Add(item.Key, item.Value);
-
-
-
string jsonstring = json.ToString();
-
byte[] jsonbyte = Encoding.UTF8.GetBytes(jsonstring);
-
Stream postStream = request.GetRequestStream();
-
postStream.Write(jsonbyte, 0, jsonbyte.Length);
-
-
-
-
-
-
res = (HttpWebResponse)request.GetResponse();
-
-
-
-
res = (HttpWebResponse)ex.Response;
-
-
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
-
string content = sr.ReadToEnd();
-
-