zoukankan      html  css  js  c++  java
  • C#客户端发送Http请求与服务端通信

    原文地址: 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&param2=xx”的形式传递的,所以代码如下

    1.  
      //url为请求的网址,param参数为需要查询的条件(服务端接收的参数,没有则为null)
    2.  
      //返回该次请求的响应
    3.  
      public string GET(string url,Dictionary<String,String> param)
    4.  
      {
    5.  
      if(param!=null) //有参数的情况下,拼接url
    6.  
      {
    7.  
      url = url + "?";
    8.  
      foreach (var item in param)
    9.  
      {
    10.  
      url = url + item.Key + "=" + item.Value+"&";
    11.  
      }
    12.  
      url = url.Substring(0, url.Length - 1);
    13.  
      }
    14.  
      HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;//创建请求
    15.  
      request.Method = "GET"; //请求方法为GET
    16.  
      HttpWebResponse res; //定义返回的response
    17.  
      try
    18.  
      {
    19.  
      res = (HttpWebResponse)request.GetResponse(); //此处发送了请求并获得响应
    20.  
      }
    21.  
      catch (WebException ex)
    22.  
      {
    23.  
      res = (HttpWebResponse)ex.Response;
    24.  
      }
    25.  
      StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
    26.  
      string content = sr.ReadToEnd(); //响应转化为String字符串
    27.  
      return content;
    28.  
      }
    • 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的方式提交即可,代码如下:

    1.  
      //url为请求的网址,param为需要传递的参数
    2.  
      //返回服务端的额响应
    3.  
      public string POST(string url, Dictionary<String, String> param)
    4.  
      {
    5.  
      HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; //创建请求
    6.  
      CookieContainer cookieContainer = new CookieContainer();
    7.  
      request.CookieContainer = cookieContainer;
    8.  
      request.AllowAutoRedirect = true;
    9.  
      //request.AllowReadStreamBuffering = true;
    10.  
      request.MaximumResponseHeadersLength = 1024;
    11.  
      request.Method = "POST"; //请求方式为post
    12.  
      request.AllowAutoRedirect = true;
    13.  
      request.MaximumResponseHeadersLength = 1024;
    14.  
      request.ContentType = "application/json";
    15.  
      JObject json = new JObject();
    16.  
      if(param.Count!=0) //将参数添加到json对象中
    17.  
      {
    18.  
      foreach(var item in param)
    19.  
      {
    20.  
      json.Add(item.Key, item.Value);
    21.  
      }
    22.  
      }
    23.  
      string jsonstring = json.ToString();//获得参数的json字符串
    24.  
      byte[] jsonbyte = Encoding.UTF8.GetBytes(jsonstring);
    25.  
      Stream postStream = request.GetRequestStream();
    26.  
      postStream.Write(jsonbyte, 0, jsonbyte.Length);
    27.  
      postStream.Close();
    28.  
      //发送请求并获取相应回应数据
    29.  
      HttpWebResponse res;
    30.  
      try
    31.  
      {
    32.  
      res = (HttpWebResponse)request.GetResponse();
    33.  
      }
    34.  
      catch (WebException ex)
    35.  
      {
    36.  
      res = (HttpWebResponse)ex.Response;
    37.  
      }
    38.  
      StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
    39.  
      string content = sr.ReadToEnd(); //获得响应字符串
    40.  
      return content;
    41.  

    C#客户端发送Http请求与服务端通信

    环境介绍

    1.软件 vs2013 
    2. 编程语言c# winform 
    3. 服务端采用java+spring,restful风格

    在客户端,通过HttpWebRequest方法,向服务端发送get,post,put和delete请求,但是由于服务端的接收参数不同,以及在具体请求下有稍微的不同,故分为以下几种情况(本文所有代码均为底层接口的形式给出,可以直接被上层具体方法调用)

    1.GET请求,服务端接收参数方式为@RequestParam

    get请求接收参数的方式通常均为@RequestParam,此时,请求参数实际上是以 “url?param1=xx&param2=xx”的形式传递的,所以代码如下

    1.  
      //url为请求的网址,param参数为需要查询的条件(服务端接收的参数,没有则为null)
    2.  
      //返回该次请求的响应
    3.  
      public string GET(string url,Dictionary<String,String> param)
    4.  
      {
    5.  
      if(param!=null) //有参数的情况下,拼接url
    6.  
      {
    7.  
      url = url + "?";
    8.  
      foreach (var item in param)
    9.  
      {
    10.  
      url = url + item.Key + "=" + item.Value+"&";
    11.  
      }
    12.  
      url = url.Substring(0, url.Length - 1);
    13.  
      }
    14.  
      HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;//创建请求
    15.  
      request.Method = "GET"; //请求方法为GET
    16.  
      HttpWebResponse res; //定义返回的response
    17.  
      try
    18.  
      {
    19.  
      res = (HttpWebResponse)request.GetResponse(); //此处发送了请求并获得响应
    20.  
      }
    21.  
      catch (WebException ex)
    22.  
      {
    23.  
      res = (HttpWebResponse)ex.Response;
    24.  
      }
    25.  
      StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
    26.  
      string content = sr.ReadToEnd(); //响应转化为String字符串
    27.  
      return content;
    28.  
      }
    • 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的方式提交即可,代码如下:

    1.  
      //url为请求的网址,param为需要传递的参数
    2.  
      //返回服务端的额响应
    3.  
      public string POST(string url, Dictionary<String, String> param)
    4.  
      {
    5.  
      HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; //创建请求
    6.  
      CookieContainer cookieContainer = new CookieContainer();
    7.  
      request.CookieContainer = cookieContainer;
    8.  
      request.AllowAutoRedirect = true;
    9.  
      //request.AllowReadStreamBuffering = true;
    10.  
      request.MaximumResponseHeadersLength = 1024;
    11.  
      request.Method = "POST"; //请求方式为post
    12.  
      request.AllowAutoRedirect = true;
    13.  
      request.MaximumResponseHeadersLength = 1024;
    14.  
      request.ContentType = "application/json";
    15.  
      JObject json = new JObject();
    16.  
      if(param.Count!=0) //将参数添加到json对象中
    17.  
      {
    18.  
      foreach(var item in param)
    19.  
      {
    20.  
      json.Add(item.Key, item.Value);
    21.  
      }
    22.  
      }
    23.  
      string jsonstring = json.ToString();//获得参数的json字符串
    24.  
      byte[] jsonbyte = Encoding.UTF8.GetBytes(jsonstring);
    25.  
      Stream postStream = request.GetRequestStream();
    26.  
      postStream.Write(jsonbyte, 0, jsonbyte.Length);
    27.  
      postStream.Close();
    28.  
      //发送请求并获取相应回应数据
    29.  
      HttpWebResponse res;
    30.  
      try
    31.  
      {
    32.  
      res = (HttpWebResponse)request.GetResponse();
    33.  
      }
    34.  
      catch (WebException ex)
    35.  
      {
    36.  
      res = (HttpWebResponse)ex.Response;
    37.  
      }
    38.  
      StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
    39.  
      string content = sr.ReadToEnd(); //获得响应字符串
    40.  
      return content;
    41.  
      }
     
     
  • 相关阅读:
    echarts 立体图
    css 设置边框边角
    PS2020 快速设置文字渐变
    idea 2019 永久破解
    使用VUE+element ui 实现输入框 占位符自动补全功能
    纯css 设置隔行样式
    CSS 设置float:left 导致后面元素错乱问题
    c primer plus 4编程练习
    c语言中以八进制数表示字符、并输出
    c语言中printf()函数的返回值
  • 原文地址:https://www.cnblogs.com/lizhigang/p/15578962.html
Copyright © 2011-2022 走看看