各种接口常用助手类

平时在项目中肯定会涉及到各种第三方接口,接口对接的过程重要接口需要设计验签等操作,会涉及对接口提交数据的各种序列化

第一种将地址栏格式的请求参数A=a&B=b这样格式的字符串转换为字典

此方法可以有规避在value中出现子项的&和=使用

 /// <summary>
        /// 通用_根据请求字符串序列化字典
        /// </summary>
        /// <param name="txtParams">请求字符串</param>
        /// <returns></returns>
        public static Dictionary<string, string> GetDictionaryBystr(string txtParams)
        {
            Encoding encoding=Encoding.UTF8;
            Dictionary<String, String> Dictionary = new Dictionary<String, String>();
            int len = txtParams.Length;
            StringBuilder temp = new StringBuilder();
            char curChar;
            String key = null;
            bool isKey = true;
            bool isOpen = false;//值里有嵌套
            char openName = ''; //关闭符

            for (int i = 0; i < len; i++)
            {// 遍历整个带解析的字符串
                curChar = txtParams[i];// 取当前字符
                if (isOpen)
                {
                    if (curChar == openName)
                    {
                        isOpen = false;
                    }
                    temp.Append(curChar);
                }
                else if (curChar == '{')
                {
                    isOpen = true;
                    openName = '}';
                    temp.Append(curChar);
                }
                else if (curChar == '[')
                {
                    isOpen = true;
                    openName = ']';
                    temp.Append(curChar);
                }
                else if (isKey && curChar == '=')
                {// 如果当前生成的是key且如果读取到=分隔符
                    key = temp.ToString();
                    temp = new StringBuilder();
                    isKey = false;
                }
                else if (curChar == '&' && !isOpen)
                {// 如果读取到&分割符
                    putKeyValueToDictionary(temp, isKey, key, Dictionary, encoding);
                    temp = new StringBuilder();
                    isKey = true;
                }
                else
                {
                    temp.Append(curChar);
                }
            }
            if (key != null)
                putKeyValueToDictionary(temp, isKey, key, Dictionary, encoding);
            return Dictionary;
        }

        private static void putKeyValueToDictionary(StringBuilder temp, bool isKey, String key, Dictionary<String, String> Dictionary, Encoding encoding)
        {
            if (isKey)
            {
                key = temp.ToString();
                if (key.Length == 0)
                {
                    throw new System.Exception("QString format illegal");
                }
                Dictionary[key] = "";
            }
            else
            {
                if (key.Length == 0)
                {
                    throw new System.Exception("QString format illegal");
                }
                //Dictionary[key] = HttpUtility.UrlDecode(temp.ToString(), encoding);
                Dictionary[key] = temp.ToString();
            }
        }
原文地址:https://www.cnblogs.com/loyung/p/6677235.html