/// <summary> /// 獲取url中的查詢字符串參數 /// </summary> public static NameValueCollection ExtractQueryParams(string url) { int startIndex = url.IndexOf("?"); NameValueCollection values = new NameValueCollection(); if (startIndex <= 0) return values; string[] nameValues = url.Substring(startIndex + 1).Split('&'); foreach (string s in nameValues) { string[] pair = s.Split('='); string name = pair[0]; string value = string.Empty; if (pair.Length > 1) value = pair[1]; values.Add(name, value); } return values; }
/// <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( " gb2312 " ); } return HttpUtility.UrlDecode(str, encoding); }
using System.Security.Cryptography;
using System.Collections.Specialized; 編碼
NameValueCollection col = ExtractQueryParams("partnerId=12546&action=getProduct×tamp=1560850762&nonce=8ObVcFPmverVgKth&sign=ea93f793f67e3a442fd8ad2bdf61e70e&data=FxsFyiX2s7M3zsnRzpkvGe6oOeLvmN1Q0u24cC1YX3PBU8fE9KA%2BOwU2RFD%2F4yhglONDiFC3te5Cqe0%2FLrfkZQcsUefPieWWo8K2GlOAA4OQBu3Y9YX8xkWTNPPcWjHLN%2BMd%2BOR%2B6fHL0AxQgKBNdTSKdOR9fo9WUZa1Snvh%2Bww%3D"); string searchKey = col["action"]; Response.Write(searchKey);