using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Reflection; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace WX.CRM.Common.Employee { public class ApiDock { public ApiDock(string _AppId, string _Secret) { this.AppId = _AppId; this.Secret = _Secret; } private string AppId { get; set; } private string Secret { get; set; } public void GetApidResult(string url, Dictionary param) { //AppId = "crm_tg_dng8"; //Secret = "d7AQhPg9PX+K80n2rDZthkzB9HFf4rwwE7fEQCmByFo="; //默认参数 //拼接验证头 Dictionary header = new Dictionary(); var timestamps = "2022-06-22T18:50:51+08";// DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszz"); string ContentMd5 = GetContentMd5(param);//获取md5加密值 var sign = GetSign(timestamps, ContentMd5, "get"); var authorization = $"acs {AppId}:{sign}"; header.Add("Authorization", authorization); //header.Add("Date", timestamps); header.Add("Content-MD5", ContentMd5); //请求 var req = Get(url, param, header, timestamps); LogHelper.Info("wechat:" + req); //返回 } /// /// 内置请求 /// /// /// private string Get(string url, Dictionary param, Dictionary header, string timestamps) { //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); ////请求设置 //request.Method = "GET"; //request.ContentType = "application/json;charset=UTF-8"; //request.Timeout = 3000;//设置超时时间 ////拼接请求头 //foreach (var item in header) //{ // request.Headers.Add(item.Key, item.Value); //} //MethodInfo priMethod = request.Headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic); //priMethod.Invoke(request.Headers, new[] { "Date", timestamps }); ////拼接参数 //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); // } //} ////提交请求 //HttpWebResponse response; //try //{ // response = (HttpWebResponse)request.GetResponse(); //} //catch (WebException ex) //{ // response = (HttpWebResponse)ex.Response; //} ////解析请求结果 //using (Stream myResponseStream = response.GetResponseStream()) //{ // StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); // string retString = myStreamReader.ReadToEnd(); // myStreamReader.Close(); // //记录请求情况 // LogHelper.Info($"url:{url},param:{JsonConvert.SerializeObject(param)},header:{JsonConvert.SerializeObject(header)},req:{retString}"); // return retString; //} var urlParam = string.Join("&", param.Select(m => m.Key + "=" + m.Value)); if (url.IndexOf('?') > -1) { url = url + urlParam; } else { url = (url + "?" + urlParam); } HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url)); MethodInfo priMethod = webReq.Headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic); priMethod.Invoke(webReq.Headers, new[] { "Date", timestamps }); foreach (var item in header) { webReq.Headers.Add(item.Key, item.Value); } webReq.Method = "get"; //webReq.ContentType = "application/x-www-form-urlencoded"; webReq.ContentType = "application/json;charset=UTF-8"; //webReq.Timeout = 3000;//设置超时时间 HttpWebResponse response = (HttpWebResponse)webReq.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); var _Result = sr.ReadToEnd(); sr.Close(); response.Close(); return _Result; } /// /// 获取时间戳 /// /// private string GetTimeStamp() { TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString(); } /// /// 计算签名 /// /// /// /// private string GetSign(string timestamps, string ContentMd5, string method) { var json = "{\"Method\":\"" + method + "\",\"Date\":\"" + timestamps + "\",\"Content-MD5\":\"" + ContentMd5 + "\"}"; var bx = JsonConvert.DeserializeObject(json); //md5 加密 return _md5(JsonConvert.SerializeObject(bx)); } public string GetContentMd5(Dictionary param) { //一次排序 var newP = param.OrderBy(m => m.Key).ToDictionary(m => m.Key, n => n.Value); var pJosn = JsonConvert.SerializeObject(newP); //md5 加密 return _md5(pJosn); } /// /// 计算 md5 /// /// /// private string _md5(string enCode) { string md5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(enCode, "MD5"); //string res = ""; //byte[] data = Encoding.GetEncoding("utf-8").GetBytes(enCode); //MD5 md5 = new MD5CryptoServiceProvider(); //byte[] bytes = md5.ComputeHash(data); //for (int i = 0; i < bytes.Length; i++) //{ // res += bytes[i].ToString("x2"); //} return Utility.StrToBase64(md5); } } }