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 + HttpUtility.UrlEncode(kvp.Key) + "=" + HttpUtility.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);
}
}
我是原著stephendeng,轉載請說明
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/231671.html
標籤:ASP.NET
上一篇:SQL Server 批量插入資料方案 SqlBulkCopy 的簡單封裝,讓批量插入更方便
下一篇:c# 生成json的方法系列
