zoukankan      html  css  js  c++  java
  • 自动按参数首字母排序参数

     public static string FormatBizQueryParaMap(Dictionary<string, string> paraMap, bool urlencode)
        {
            string buff = "";
            try
            {
                var result = from pair in paraMap orderby pair.Key select pair;
    
                foreach (KeyValuePair<string, string> pair in result)
                {
                    if (pair.Key != "")
                    {
                        string key = pair.Key;
                        string val = pair.Value;
                        if (urlencode)
                        {
                            val = System.Web.HttpUtility.UrlEncode(val);
                        }
                        buff += key.ToLower() + "=" + val + "&";
    
                    }
                }
    
                if (buff.Length == 0 == false)
                {
                    buff = buff.Substring(0, (buff.Length - 1) - (0));
                }
            }
            catch (Exception e)
            {
                //throw new SDKRuntimeException(e.Message);
            }
            return buff;
        }


    调用上面方法案例:

    Dictionary<string, string> dic = new Dictionary<string, string>();
    dic.Add("appid", appid);
    dic.Add("outtrxid", outtrxid);
    dic.Add("trxcode", trxcode);
    dic.Add("trxid", trxid);
    dic.Add("trxamt", Amount);
    dic.Add("trxdate", trxdate);
    dic.Add("paytime", paytime);
    dic.Add("chnltrxid", chnltrxid);
    dic.Add("trxstatus", Succeed);

    string _WaitSign = FormatBizQueryParaMap(dic, false);

    Response.Write(_WaitSign);

    另有很多数据请求后返回都是JSON的格式。附加一个读取JSON的东西

        //post提交请求返回内容
    public static string RequestData(string POSTURL, string PostData)
        {
            //发送请求的数据
            WebRequest myHttpWebRequest = WebRequest.Create(POSTURL);
            myHttpWebRequest.Method = "POST";
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] byte1 = encoding.GetBytes(PostData);
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
            myHttpWebRequest.ContentLength = byte1.Length;
            Stream newStream = myHttpWebRequest.GetRequestStream();
            newStream.Write(byte1, 0, byte1.Length);
            newStream.Close();
    
            //发送成功后接收返回的XML信息
            HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse();
            string lcHtml = string.Empty;
            Encoding enc = Encoding.GetEncoding("UTF-8");
            Stream stream = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(stream, enc);
            lcHtml = streamReader.ReadToEnd();
            return lcHtml;
        }

    //将JSON数据转成类的形式
    public static T JsonToModel<T>(string jsonString) { using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString))) { try { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); T jsonObject = (T)serializer.ReadObject(ms); return jsonObject; } catch (Exception ex) { throw ex; } finally { ms.Close(); ms.Dispose(); } } }

    例子:

    string jsondate = RequestData(SumbitUrl, senddata);
    如jsondate="{"aaa":"1",
    "aaa":"1","bbb":"2","ccc":"3"}";
    创建一个类如下
    public class Test{
    public string aaa;

    public string bbb;
    public string ccc; }
    使用的时候就可以直接调用了
     Test _Test = JsonToModel<Test>(
    jsondate);
    获取其中的值就用对应的字段就可以了
    获取aaa=_Test.aaa;

     
     
  • 相关阅读:
    JAVA下使用 连接sqlserver 驱动包
    Windows 7 、Windows Server 2008 和 Windows Server 2008 R2 的支持结束
    VBoxManage命令详解
    端口扫描之王——nmap入门精讲
    rehat-server7常见服务安装与配置总结
    mysql的安装和密码管理、mysql初始密码查找、密码修改、mysql登录
    vim常用命令总结 (转)
    关于《Python绝技:运用Python成为顶级黑客》的学习笔记
    常用MySQL图形化管理工具
    Chrome谷歌浏览器离线安装包下载
  • 原文地址:https://www.cnblogs.com/yuanye0918/p/5741100.html
Copyright © 2011-2022 走看看