zoukankan      html  css  js  c++  java
  • c#实现php的http_build_query功能

    php的http_build_query不得不说很好用,用c#实现它,过程稍长

    http_build_query方法:

    public static string http_build_query(Dictionary<string, string> dict = null)
    {
    if (dict == null)
    {
    return "";
    }
    string QueryString=string.Empty;

    foreach (KeyValuePair<string, string> kvp in dict)
    {
    QueryString = QueryString + System.Net.WebUtility.UrlEncode(kvp.Key) + "=" + System.Net.WebUtility.UrlEncode(kvp.Value) + "&";
    }
    QueryString = QueryString.Substring(0, QueryString.Length - 1);
    return QueryString;
    }

    调用过程:

    protected void testhttp_build_query_Click(object sender, EventArgs e)
    {
    try
    {
    string secret = "1e7f7231-12b1-86ec"; //密钥
    var DataDictionary = new Dictionary<string, string>();
    DataDictionary.Add("currency", "HKD");
    DataDictionary.Add("amount", "100");
    DataDictionary.Add("return_url", "www.baidu.com/api"); //显示支付成功的界面,应该做多一个页面叫payment_return.aspx
    DataDictionary.Add("customer_first_name", "stephen");
    DataDictionary.Add("customer_last_name", "");
    DataDictionary.Add("customer_address", "");
    DataDictionary.Add("customer_phone", "90472886");
    DataDictionary.Add("customer_email", "");
    DataDictionary.Add("customer_state", "");
    DataDictionary.Add("customer_country", "CN");
    DataDictionary.Add("network", "alipay");
    DataDictionary.Add("subject", "Jiyun Fee");
    Dictionary<string, string> dic_SortedByKey = DataDictionary.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value); //对key排从小到大的升序,类似php ksort($fields);
    var strrrrr = http_build_query(dic_SortedByKey);

    showDesString.Text = strrrrr;
    }
    catch (Exception err)
    {
    Response.Write(err.Message);
    }
    }

    修正下,不要使用HttpUtility.UrlEncode做Urlencode,因为它转出来的是小写,应该使用System.Net.WebUtility.UrlEncode,而java,php使用urlencode转化出来的是大写,

     我是原著stephendeng,转载请说明

  • 相关阅读:
    hive中如何查询除了其中某个字段剩余所有字段
    查找出不同环境下同一张表的数据差异
    pycharm中导入包失败的解决办法
    hive如何获取当前时间
    python-匿名函数
    Tensorflow报错:OMP: Error #15: Initializing libiomp5.dylib, but found libiomp5.dylib already initialized.
    Tensorflow中Tensor对象的常用方法(持续更新)
    Numpy中的广播机制,数组的广播机制(Broadcasting)
    重装conda
    matplotlib作图一例
  • 原文地址:https://www.cnblogs.com/alannever/p/14520289.html
Copyright © 2011-2022 走看看