using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; namespace WX.CRM.Common { public class CMSApiHelper { private string url = "http://localhost:30139/"; public CMSApiResult PushImportData(Dictionary param) { var pushUrl = $"{url}/Import/ImportData.html"; var req = Post(pushUrl, param, new Dictionary()); return JsonConvert.DeserializeObject>(req); } public CMSApiResult SubmitImport(Dictionary param) { var pushUrl = $"{url}/Import/ImportSubmit.html"; var req = Post(pushUrl, param, new Dictionary()); return JsonConvert.DeserializeObject>(req); } /// /// 内置请求 /// /// /// private string Post(string url, Dictionary param, Dictionary header) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //请求设置 request.Method = "POST"; request.ContentType = "application/json;charset=UTF-8"; request.Timeout = 2000; //拼接请求头 foreach (var item in header) { request.Headers.Add(item.Key, item.Value); } //拼接参数 if (param.Any()) { var pStr = JsonConvert.SerializeObject(param); byte[] data = Encoding.UTF8.GetBytes(pStr); using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { //解析请求结果 using (Stream myResponseStream = response.GetResponseStream()) { StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); return retString; } } } catch (Exception ex) { return ex.Message; } } } public class CMSApiResult { public string msg { get; set; } public int code { get; set; } public T data { get; set; } } public class PushDataRes { public string pushCode { get; set; } } }