码迷,mamicode.com
首页 > 编程语言 > 详细

自动按参数首字母排序参数

时间:2016-08-05 15:29:29      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

 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;

 

 

 

 

 

 

自动按参数首字母排序参数

标签:

原文地址:http://www.cnblogs.com/yuanye0918/p/5741100.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!