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();
                }
            }
  • 相关阅读:
    Netty源码分析——准备
    Netty入门
    Netty源码分析——EventLoopGroup建立
    三层架构搭建(asp.net mvc + ef)
    Springboot 1.5.x 集成基于Centos7的RabbitMQ集群安装及配置
    Springboot 2.0.x 集成基于Centos7的Redis集群安装及配置
    Springboot 2.0.x 引入链路跟踪Sleuth及Zipkin
    JAVA编码 —— 字符串关键字内容替换
    使用java发送QQ邮件的总结
    Docker原理探究
  • 原文地址:https://www.cnblogs.com/loyung/p/6677235.html
Copyright © 2011-2022 走看看