标签:
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