zoukankan      html  css  js  c++  java
  • 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; }
       }
    
  • 相关阅读:
    session判断重复提交
    logback日志配置
    quartz动态job工具类 serviceh注入问题
    mysql10061登录失败错误解决方案
    安装步骤
    无法启动此程序,因为计算机丢失MSVCP120.dll
    Bigdecimal: Non-terminating decimal expansion; no exact representable decimal result.
    replace()函数用法
    随机读取表中一条数据
    oracle table()函数
  • 原文地址:https://www.cnblogs.com/bemad/p/14846358.html
Copyright © 2011-2022 走看看