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;

     
     
  • 相关阅读:
    Oracle 按一行里某个字段里的值分割成多行进行展示
    Property or method "openPageOffice" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by
    SpringBoot 项目启动 Failed to convert value of type 'java.lang.String' to required type 'cn.com.goldenwater.dcproj.dao.TacPageOfficePblmListDao';
    Maven 设置阿里镜像
    JS 日期格式化,留作参考
    JS 过滤数组里对象的某个属性
    原生JS实现简单富文本编辑器2
    Chrome控制台使用详解
    android权限(permission)大全
    不借助第三方网站四步实现手机网站转安卓APP
  • 原文地址:https://www.cnblogs.com/yuanye0918/p/5741100.html
Copyright © 2011-2022 走看看