namespace OASystem.API.CMBPayBusiness { public static class Common { public static string GetSignContent(Dictionary sortedParams) { //字典排序,并用&进行拼接,例如,b=a,a=b,拼接后变成a=b&b=a StringBuilder content = new StringBuilder(); //利用linq进行字典升序的排序 var dicSort = from objDic in sortedParams orderby objDic.Key select objDic; foreach (KeyValuePair kvp in dicSort) { string key = kvp.Key; string value = kvp.Value; if (null != key && null != value) { content.Append(key + "=" + value + "&"); } } if (content.Length != 0) { content.Remove(content.Length - 1, 1); } return content.ToString(); } public static string GetSign(Dictionary sortedParams) { //排序拼接参数 string signContent = GetSignContent(sortedParams); //加签 string sign = GmUtil.generateSmSign(signContent); return sign; } public static bool VerifySign(Dictionary sortedParams) { //获取返回的sign string returnSign = sortedParams["sign"]; //删除返回的sign sortedParams.Remove("sign"); //排序拼接参数 string signContent = GetSignContent(sortedParams); //验签 bool flag = GmUtil.verifySmSign(signContent, returnSign); return flag; } public static Dictionary PostForEntity(string url, string param, Dictionary dic = null) { HttpWebRequest request;//仅作展示,这个方法用于发送网络请求可能有性能问题 request = (HttpWebRequest)WebRequest.Create(url); // 以POST的方式提交 request.Method = "POST"; // 以json的方式提交 request.ContentType = "application/json;charset=UTF-8"; // 请求头部 if (dic != null && dic.Count != 0) { foreach (var item in dic) { request.Headers.Add(item.Key, item.Value); } } byte[] payload; // 请求参数 payload = Encoding.UTF8.GetBytes(param); request.ContentLength = payload.Length; string strValue = ""; try { Stream writer = request.GetRequestStream(); writer.Write(payload, 0, payload.Length); writer.Close(); HttpWebResponse response; response = (HttpWebResponse)request.GetResponse(); Stream s; s = response.GetResponseStream(); string StrDate = ""; StreamReader Reader = new StreamReader(s, Encoding.UTF8); while ((StrDate = Reader.ReadLine()) != null) { strValue += StrDate; } } catch (Exception e) { Console.WriteLine("post请求报错:" + e.Message); return null; } //JavaScriptSerializer jss = new JavaScriptSerializer(); return JsonConvert.DeserializeObject>(strValue); } /// /// 返回网络时间 --北京时间 /// /// public static DateTime GetBeijingTime() { WebRequest request = null; WebResponse response = null; WebHeaderCollection headerCollection = null; string datetime = string.Empty; try { request = WebRequest.Create("https://www.baidu.com"); request.Timeout = 3000; request.Credentials = CredentialCache.DefaultCredentials; response = request.GetResponse(); headerCollection = response.Headers; foreach (var h in headerCollection.AllKeys) { if (h == "Date") { datetime = headerCollection[h]; break; } } return Convert.ToDateTime(datetime); } catch (Exception) { return DateTime.Now; } finally { if (request != null) { request.Abort(); } if (response != null) { response.Close(); } if (headerCollection != null) { headerCollection.Clear(); } } } #region Http请求封装 public static Dictionary GetHeader(Dictionary sortedParams) { //long timestamp = ConvertDateTime(System.DateTime.Now) / 10000000; long timestamp = ConvertDateTime(GetBeijingTime()) / 10000000; // 组apiSign加密Map Dictionary apiSign = new Dictionary(); apiSign.Add("appid", BaseConfig.APPID); apiSign.Add("secret", BaseConfig.APP_SECRET); apiSign.Add("sign", sortedParams["sign"]); apiSign.Add("timestamp", "" + timestamp); // MD5加密 string MD5Content = GetSignContent(apiSign); string apiSignString = GetMD5(MD5Content, "UTF-8"); // 组request头部Map Dictionary apiHeader = new Dictionary(); apiHeader.Add("appid", BaseConfig.APPID); apiHeader.Add("timestamp", "" + timestamp); apiHeader.Add("apisign", apiSignString); return apiHeader; } private static long ConvertDateTime(DateTime time) { DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0)); return time.Ticks - startTime.Ticks; } public static string GetMD5(string data, string charset) { //MD5加密,同样的字符串在每次加密后的字符串是一样的 byte[] bData = Encoding.GetEncoding(charset).GetBytes(data); MD5CryptoServiceProvider myMD5 = new MD5CryptoServiceProvider(); bData = myMD5.ComputeHash(bData); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < bData.Length; i++) { //十六进制转成小写的英文字符 sBuilder.Append(bData[i].ToString("x2")); } return sBuilder.ToString(); } #endregion #region 二维码图片处理 /// /// 调用此函数后使此两种图片合并,类似相册,有个 /// 背景图,中间贴自己的目标图片 /// /// 粘贴的源图片 /// 粘贴的目标图片 public static Image CombinImage(Image imgBack, string destImg) { Image img = Image.FromFile(destImg); //照片图片 if (img.Height != 65 || img.Width != 65) { img = KiResizeImage(img, 65, 65, 0); } Graphics g = Graphics.FromImage(imgBack); g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height); //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高); //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框 //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高); g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2, img.Width, img.Height); GC.Collect(); return imgBack; } /// /// Resize图片 /// /// 原始Bitmap /// 新的宽度 /// 新的高度 /// 保留着,暂时未用 /// 处理以后的图片 public static Image KiResizeImage(Image bmp, int newW, int newH, int Mode) { try { Image b = new Bitmap(newW, newH); Graphics g = Graphics.FromImage(b); // 插值算法的质量 g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel); g.Dispose(); return b; } catch { return null; } } //public bool IsReusable //{ // get // { // return false; // } //} #endregion #region 通知参数处理 public static Dictionary str2Map(string str) { Dictionary result = new Dictionary(); string[] results = str.Split('&'); if (results != null && results.Length > 0) { for (int var = 0; var < results.Length; ++var) { string pair = results[var]; string[] kv = pair.Split('='); if (kv != null && kv.Length == 2) { result.Add(kv[0], kv[1]); } } } return result; } public static string decode(string str) { string result = null; if (str != null) { result = Uri.UnescapeDataString(str); } return result; } public static string NotifySign(string requestJsonStr) { return ""; } #endregion } }