tnblog
首页
视频
资源
登录

后台请求swagger-web api数据(含token标头的添加)

4573人阅读 2022/4/8 16:34 总访问:39070 评论:0 收藏:0 手机
分类: .NET

本文主要为了实现通过.NET代码请求swagger接口获取json字符串
然后解析拿到需要的数据
封装方法,GET及POST方法都兼容

  /// <param name="url">接口地址</param>
  /// <param name="token">标头</param>
  /// <param name="Type">请求方式</param>
  // token 看个人使用情况,一般get方法不需要设置token 但是设置了也不会影响查询结果
  
 public static string HttpApi(string url, string token = "", string Type = "GET", string body = null)
        {
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);


            request.Method = Type;
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.ContentType = "application/json";
            if (token != "")
            {
                request.Headers.Add("Authorization", "Bearer " + token);
            }
            if (body != null)
            {
                byte[] buffer = encoding.GetBytes(body);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
            }



            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }

注意:如果接口是GET请求方式,方法里面是post会请求失败,所以需要 统一接口和方法的请求方式

  • 获取token

 public static string HttpApiToken(string url, string body)
        {
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.ContentType = "application/json";

            byte[] buffer = encoding.GetBytes(body);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }

像encoding以及accept这些参数配置出入不大,如果本文章的不兼容可以自行修改

  • 调用

// 调用获取token的接口  ip地址,接口地址和后面的端口号自行修改及定义
 string logingUrl = HttpApiToken($"http://{ip}:10140/.../.../.../...", "{userName:\"admin\",password:\"111111\"}");
            dynamic response = Newtonsoft.Json.JsonConvert.DeserializeObject(logingUrl);//对象转换
            token = response.data.token;
 //调用获取json接口 和获取token出入不大
 //本文分别举例post和get两种不一样的请求方式
 //post
  string RYQYurl = $"http://{serverStr}:10140/api/Business/BaseArea/GetList";
   string data = HttpApi(RYQYurl, token, "POST", "{参数条件}");
   dynamic AreaCategoryJSON = Newtonsoft.Json.JsonConvert.DeserializeObject(data);//对象转换
    var AreaCategoryList = AreaCategoryJSON.data; //这个位置 点data和点什么看接口返回数据格式
    
    //可能单独存在data里面,也有可能是存在data里面的list里面,只需要多点一下list就可以了>。。。.data.list
    //解析
     foreach (var item in AreaCategoryList)
      {
      string name=item.name;
      //string 自定义名称=item.接口返回数据名称
      }
      
      
 //GET
  string PersonnelLocationUrl = $"http://{ip}:10140/api/Business/PersonnelLocation/GetPersonnelLocationList";
                string PersonnelLocationList = HttpApi(PersonnelLocationUrl, token, "GET");
                dynamic PersonnelLocationJSON = Newtonsoft.Json.JsonConvert.DeserializeObject(PersonnelLocationList);//对象转换
                //解析
                var PersonnelLocations = PersonnelLocationJSON.data;
                //遍历 和post一样

需要注意的是,get请求条件在路径里面  post请求在调用方法时给定的body值
ok 本章结束,谢谢观看

评价
与其用时间去get一个未知的以后,不如做好现在,set自己一个好的以后
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术