zoukankan      html  css  js  c++  java
  • 各种接口常用助手类

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

    第一种将地址栏格式的请求参数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();
                }
            }
  • 相关阅读:
    对《分享一下自己用c++写的小地图》一文的补充
    大神的Blog挂了,从Bing快照里复制过来的备份
    如何创建独立的UE4服务端
    打包如何打包额外文件,比如Sqlite数据库的db文件
    关于如何通过定义自己的CameraManager来控制视角
    Slate中绑定动态数据
    分享一下自己用c++写的小地图
    2016.8看到的一些有用的文章
    如何在插件中添加Actor类
    如何使用的Ue4自带的SQLiteSupport
  • 原文地址:https://www.cnblogs.com/loyung/p/6677235.html
Copyright © 2011-2022 走看看