• C# 调用接口实例


    1.方法:
    ` public static string GetApiData(string url,string token,Dictionary<string, object> dic)
    {
    string result = "";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";
    request.ContentType = "application/json";
    request.Headers.Add("X-Access-Token", token);//添加token验证

            #region 添加Post 参数
            string parJsonStr = JsonConvert.SerializeObject(dic);
    
            byte[] data = Encoding.UTF8.GetBytes(parJsonStr);
            request.ContentLength = data.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
    
            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
    
            return result;
        }`
    

    2.调用

               dicM.Add("accountId", accId);
                dicM.Add("brCode", brCode);
                dicM.Add("beginBuildDate", $"{beginDate.ToString("yyyy-MM-dd HH:mm:ss").Split(' ')[0]}");
                dicM.Add("endBuildDate", $"{endDate.ToString("yyyy-MM-dd HH:mm:ss").Split(' ')[0]}");
                dicM.Add("page", $"{pageIndex}");
                dicM.Add("rows", $"{pageSize}");
                string resultM = GetApiData(url, token, dicM);
    

    3.数据解析
    RespData<KLTLISTM> mData = Newtonsoft.Json.JsonConvert.DeserializeObject<RespData<KLTLISTM>>(resultM);

    4.相应的实体

    public class RespData<T> where T:class
       {
           public int code { get; set; }
           public List<T> data { get; set; }
           public string message { get; set; }
           public object rows { get; set; }
           public int total { get; set; }
       }
    
  • 相关阅读:
    MYSQL 使用DBI
    mysql 更改数据目录
    Error Code: 1360
    org.hibernate.exception.GenericJDBCException: Could not open connection
    Error: Dynamic is undefined
    Unhandled event loop exception No more handles
    Count:858org.apache.jasper.JasperException: Unable to compile class for JSP
    Mysql --skip-grant-table
    第24章-启动 停止 和配置mysql
    Linux 6.2 x86_64 安装ipvs
  • 原文地址:https://www.cnblogs.com/bemad/p/14846358.html
Copyright © 2020-2023  润新知