常用的辅助工具类
public static class Utility { //MD5加密 public static string GetMD5_32(string s) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(s)); StringBuilder sb = new StringBuilder(); foreach (byte b in data) sb.Append(b.ToString("X2")); return sb.ToString().ToUpper(); } //获取签名 public static string GetSign<T>(T t, string key) { var items = t.GetType().GetProperties().Where(ii => !string.IsNullOrEmpty(ii.GetValue(t)?.ToString())).Select(ii => new { Key = ii.Name, Value = ii.GetValue(t).ToString() }).OrderBy(ii => ii.Key).ToList(); var item = items.FirstOrDefault(j => j.Key == "sign"); string sign = GetMD5_32($"{ string.Join("&", items.Select(ii => ii.Key + "=" + ii.Value))}" + key); return sign; //string str = ""; ////str += t.GetType().GetProperty("appkey").GetValue(t).ToString()+ t.GetType().GetProperty("hotelid").GetValue(t).ToString()+ t.GetType().GetProperty("loc").GetValue(t).ToString()+ t.GetType().GetProperty("ts").GetValue(t).ToString(); //List<string> items = t.GetType().GetProperties().Where(ii => !string.IsNullOrEmpty(ii.GetValue(t)?.ToString())).Select(ii => ii.GetValue(t).ToString().ToUpper()).ToList(); //items.Sort(); //for(int i=0;i<items.Count;i++) //{ // str += items[i]; //} //string sign = GetMD5_32(str); //return sign; } //发送post请求 public static T HttpPost<T>(string url, string data) { WebClient wc = new WebClient(); wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); var ret = wc.UploadData(url, Encoding.UTF8.GetBytes(data)); return JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(ret)); } //去掉json序列化中值为空的字段 public static string JsonStrGet(object data) { var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; return JsonConvert.SerializeObject(data, Formatting.Indented, jsonSetting); } //生成时间戳 public static long GetTimeStamp() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds); } //获取七位数据数 public static string RandomGet() { Random random = new Random(); string s = ""; for (int i = 0; i < 7; i++) { s += random.Next(10).ToString(); } return s; }