using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Security.Cryptography; namespace TelpoPush.Position.Worker.Common { public class Utils { public static MultipartFormDataContent GetMultipartFormDataContent(Dictionary dic, string appId, ref Dictionary outDic) { MultipartFormDataContent mfdc = new MultipartFormDataContent(); StringBuilder sb = new StringBuilder(); if (dic != null && dic.Count > 0) { var dicOrderBy = dic.OrderBy(z => z.Key); foreach (KeyValuePair kv in dicOrderBy) { sb.Append($"{kv.Key}={kv.Value.ToString()}&"); mfdc.Add(new StringContent(kv.Value.ToString()), kv.Key);//参数, 内容在前,参数名称在后 } } string signStr = $"{sb.ToString().Trim('&')}{appId}"; byte[] bytes = Encoding.UTF8.GetBytes(signStr); byte[] hash = SHA256.Create().ComputeHash(bytes); StringBuilder builder = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { builder.Append(hash[i].ToString("X2")); } string sign = builder.ToString().ToLower(); dic.Add("sign", sign); mfdc.Add(new StringContent(sign), "sign");//参数, 内容在前,参数名称在后 outDic = dic; return mfdc; } } }