Page_Load接收随机参数放到字典类中,可以用作签名。
普通的接收url的参数可以用作下面这种模式:
int appid =Convert.ToInt32(param["appid"]);
但是上面这种模式又有一定的限制,即前提是要知道存在appid这个参数名,那么问题来了,如果url是随机传参怎么办,怎么知道url的参数名和值那呢?
首先在 Page_Load事件里,写如下代码:
string queryString = HttpContext.Current.Request.Url.Query;//行如这样的 ?a=123&ak=jakd&kl=mm&cv=909
// queryString = queryString.Replace("amp;", "");//有时候接收的时候,是这样的 ?a=123amp;&ak=jakdamp;&kl=mmamp;&cv=909,就需要替换掉amp;
NameValueCollection col = GetQueryString(queryString);
Dictionary<string, string> param = new Dictionary<string, string>();
for (int i = ; i < col.Count; i++)
{ param.Add(col.GetKey(i), col[i].ToString());//关键代码
}
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 = ; i < count; i++)
{
int startIndex = i;
int index = -;
while (i < count)
{
char item = queryString[i];
if (item == '=')
{
if (index < )
{
index = i;
}
}
else if (item == '&')
{
break;
}
i++;
}
string key = null;
string value = null;
if (index >= )
{
key = queryString.Substring(startIndex, index - startIndex);
value = queryString.Substring(index + , (i - index) - );
}
else
{
key = queryString.Substring(startIndex, i - startIndex);
}
if (isEncoded)
{
result[MyUrlDeCode(key, encoding)] = MyUrlDeCode(value, encoding);
}
else
{
result[key] = value;
}
if ((i == (count - )) && (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 = ; 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 = ; k < bt.Length; k++)
{
int ascii = (short)(bt[k]);//计算每个字节的ascii码值
builder.Append("%" + Convert.ToString(ascii, ).ToUpper());
} }
}
return builder.ToString();
}
将param 排序,该方法可以扩展
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(, query_string.Length - );
query_string = UrlEncode(query_string, Encoding.UTF8);
return strs + query_string; }