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; }
}