• 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;
    
    }
  • 相关阅读:
    [CC-STREETTA]The Street
    [CF115E]Linear Kingdom Races
    [洛谷P3987]我永远喜欢珂朵莉~
    [POI2012]Squarks
    [TC6194]AllWoundUp
    [CF434D]Nanami's Power Plant
    [CF126D]Fibonacci Sums/[BJOI2012]最多的方案
    [HZOI 2016]我们爱数数
    [COGS2427][HZOI 2016]seq
    Ynoi2018 天降之物
  • 原文地址:https://www.cnblogs.com/Serialport/p/4137455.html
Copyright © 2020-2023  润新知