Page_Load接收随机参数放到字典类中

Page_Load接收随机参数放到字典类中,可以用作签名。
普通的接收url的参数可以用作下面这种模式:
  int appid =Convert.ToInt32(param["appid"]);
但是上面这种模式又有一定的限制,即前提是要知道存在appid这个参数名,那么问题来了,如果url是随机传参怎么办,怎么知道url的参数名和值那呢?
首先在 Page_Load事件里,写如下代码:

 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,那么就可以这样接收值
View Code

 

/// <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 排序,该方法可以扩展

 

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;

}

 

转载于:https://www.cnblogs.com/Serialport/p/4137455.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值