网络请求参数的处理,包含过滤,排序,拼接,加密
网络请求的参数过滤,排序,拼接,加密.也参考了alipay.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ICE.Common { public class WebCore { /// <summary> /// 除去数组中的空值和签名参数并以字母a到z的顺序排序 /// </summary> /// <param name="dicArrayPre">过滤前的参数组</param> /// <returns>过滤后的参数组</returns> public static Dictionary<string, string> FilterPara(SortedDictionary<string, string> dicArrayPre) { Dictionary<string, string> dicArray = new Dictionary<string, string>(); foreach (KeyValuePair<string, string> temp in dicArrayPre) { if (temp.Key.ToLower() != "sign" && temp.Key.ToLower() != "sign_type" && temp.Value != "" && temp.Value != null) { dicArray.Add(temp.Key, temp.Value); } } return dicArray; } /// <summary> /// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 /// </summary> /// <param name="sArray">需要拼接的数组</param> /// <returns>拼接完成以后的字符串</returns> public static string CreateLinkString(Dictionary<string, string> dicArray) { StringBuilder prestr = new StringBuilder(); foreach (KeyValuePair<string, string> temp in dicArray) { prestr.Append(temp.Key + "=" + temp.Value + "&"); } //去掉最後一個&字符 int nLen = prestr.Length; prestr.Remove(nLen - 1, 1); return prestr.ToString(); } private static string BuildRequestMysign(Dictionary<string, string> sPara) { //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 string prestr = CreateLinkString(sPara); //把最终的字符串签名,获得签名结果 string mysign = ""; mysign = IceEncrypt.GetMD5(prestr); return mysign; } } }
欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如果感觉对您有用,请点击推荐。您的支持,是我的动力!