Page_Load接收隨機參數放到字典類中,能夠用做簽名。
普通的接收url的參數能夠用做下面這種模式:
int appid =Convert.ToInt32(param["appid"]);
可是上面這種模式又有必定的限制,即前提是要知道存在appid這個參數名,那麼問題來了,若是url是隨機傳參怎麼辦,怎麼知道url的參數名和值那呢?
首先在 Page_Load事件裏,寫以下代碼:app
1 string queryString = HttpContext.Current.Request.Url.Query;//行如這樣的 ?a=123&ak=jakd&kl=mm&cv=909 2 // queryString = queryString.Replace("amp;", "");//有時候接收的時候,是這樣的 ?a=123amp;&ak=jakdamp;&kl=mmamp;&cv=909,就須要替換掉amp; 3 NameValueCollection col = GetQueryString(queryString); 4 Dictionary<string, string> param = new Dictionary<string, string>(); 5 for (int i = 0; i < col.Count; i++) 6 { 7 8 param.Add(col.GetKey(i), col[i].ToString());//關鍵代碼 9 } 10 string ak= param["ak"].ToString();//若是隨機參數中肯定存在ak,那麼就能夠這樣接收值
/// <summary> /// 將查詢字符串解析轉換爲名值集合. /// </summary> /// <param name="queryString"></param> /// <returns></returns> public static NameValueCollection GetQueryString(string queryString) { return GetQueryString(queryString, null, true); } /// <summary> /// 將查詢字符串解析轉換爲名值集合. /// </summary> /// <param name="queryString"></param> /// <param name="encoding"></param> /// <param name="isEncoded"></param> /// <returns></returns> public static NameValueCollection GetQueryString(string queryString, Encoding encoding, bool isEncoded) { queryString = queryString.Replace("?", ""); NameValueCollection result = new NameValueCollection(StringComparer.OrdinalIgnoreCase); if (!string.IsNullOrEmpty(queryString)) { int count = queryString.Length; for (int i = 0; i < count; i++) { int startIndex = i; int index = -1; while (i < count) { char item = queryString[i]; if (item == '=') { if (index < 0) { index = i; } } else if (item == '&') { break; } i++; } string key = null; string value = null; if (index >= 0) { key = queryString.Substring(startIndex, index - startIndex); value = queryString.Substring(index + 1, (i - index) - 1); } else { key = queryString.Substring(startIndex, i - startIndex); } if (isEncoded) { result[MyUrlDeCode(key, encoding)] = MyUrlDeCode(value, encoding); } else { result[key] = value; } if ((i == (count - 1)) && (queryString[i] == '&')) { result[key] = string.Empty; } } } return result; } /// <summary> /// 解碼URL. /// </summary> /// <param name="encoding">null爲自動選擇編碼</param> /// <param name="str"></param> /// <returns></returns> public static string MyUrlDeCode(string str, Encoding encoding) { if (encoding == null) { Encoding utf8 = Encoding.UTF8; //首先用utf-8進行解碼 string code = HttpUtility.UrlDecode(str.ToUpper(), utf8); //將已經解碼的字符再次進行編碼. string encode = HttpUtility.UrlEncode(code, utf8).ToUpper(); if (str == encode) encoding = Encoding.UTF8; else encoding = Encoding.GetEncoding("utf-8"); } return HttpUtility.UrlDecode(str, encoding); } static public string EncodeValue(string value) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < value.Length; i++) { if (Regex.IsMatch(value[i].ToString(), @"[a-zA-Z0-9!()*]")) { builder.Append(value[i].ToString()); } else { //byte[] bt = new byte[1]; byte[] bt = Encoding.UTF8.GetBytes(value[i].ToString());//中文的話,一個漢字有三個字節 for (int k = 0; k < bt.Length; k++) { int ascii = (short)(bt[k]);//計算每一個字節的ascii碼值 builder.Append("%" + Convert.ToString(ascii, 16).ToUpper()); } } } return builder.ToString(); }
將param 排序,該方法能夠擴展ide
static private string MakeSource(string method ,Dictionary<string, string> param) { string strs=method+&; string query_string = ""; List<KeyValuePair<string, string>> myList = new List<KeyValuePair<string, string>>(param); myList.Sort(delegate(KeyValuePair<string, string> s1, KeyValuePair<string, string> s2) { return s1.Key.CompareTo(s2.Key); }); foreach (KeyValuePair<string, string> pair in myList) { query_string = query_string + pair.Key + "=" + pair.Value + "&"; } query_string = query_string.Substring(0, query_string.Length - 1); query_string = UrlEncode(query_string, Encoding.UTF8); return strs + query_string; }