1 public string Get(int id) 2 { 3 4 JObject o = new JObject( 5 new JProperty("billNo", "ESL1363593563"), 6 new JProperty("billType", "24"), 7 new JProperty("pageNo", "1"), 8 new JProperty("pageSize", "10"), 9 new JProperty("queryType ", "1") 10 ); 11 //压缩str,用于得到sign的值 12 string str = "360buy_param_json" + o.ToString() + "access_token" + 123 + "app_key" + 123 + "method" + 123 + "timestamp=" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "v2.0"; 13 //完整str 14 string str1 = "360buy_param_json=" + o.ToString() + "&access_token=" + 123 + "&app_key=" + 123 + "&method=" + 123 + "×tamp=" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "&v=2.0"; 15 16 string end = "yourappSecret" + str + "yourappSecret"; 17 string md5 = StrToMD5(end);//得出来sign 18 //最后拼接url: 19 string endstr = str1 + "&sign=" + md5; 20 //经常用到url中含有中文字符的需要对其进行字符串编码和解码,所以这里就用了这个System.Web.HttpUtility.UrlEncode, 21 c#asp.net url 传递中文参数要使用 System.Web.HttpUtility.UrlEncode 而不能使用Server.UrlEncode 22 string allStr = System.Web.HttpUtility.UrlEncode(endstr, System.Text.Encoding.UTF8); 23 byte[] bufferB = Encoding.UTF8.GetBytes(allStr); 24 System.Net.ServicePointManager.DefaultConnectionLimit = 200; 25 HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("www.cctv.com"); 26 WebReq.Method = "POST"; 27 WebReq.ContentType = "application/x-www-form-urlencoded"; 28 WebReq.ContentLength = allStr.Length; 29 Stream PostData = WebReq.GetRequestStream(); 30 PostData.Write(bufferB, 0, bufferB.Length); 31 PostData.Close(); 32 33 HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse(); 34 StreamReader sr = new StreamReader(WebResp.GetResponseStream(), System.Text.Encoding.UTF8); 35 string backstr = sr.ReadToEnd(); 36 backstr = HttpUtility.UrlDecode(backstr); 37 Console.WriteLine(WebResp.StatusCode); 38 Console.WriteLine(WebResp.Server); 39 Stream Answer = WebResp.GetResponseStream(); 40 StreamReader _Answer = new StreamReader(Answer); 41 Console.WriteLine(_Answer.ReadToEnd()); 42 return _Answer.ReadToEnd(); 43 44 }
2 { 3 return val.TrimStart(c).TrimEnd(c); 4 } 5 6 private static string GetSignature(IDictionary<string, string> parameters, string secret) 7 { 8 // 先将参数以其参数名的字典序升序进行排序 9 IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters); 10 IEnumerator<KeyValuePair<string, string>> iterator = sortedParams.GetEnumerator(); 11 12 // 遍历排序后的字典,将所有参数按"keyvaluekeyvalue"格式拼接在一起 13 StringBuilder basestring = new StringBuilder(); 14 while (iterator.MoveNext()) 15 { 16 string key = iterator.Current.Key; 17 string value = iterator.Current.Value.ToString(); 18 if (!string.IsNullOrEmpty(key) && value != null) 19 { 20 basestring.Append(key).Append(value); 21 } 22 } 23 24 // 使用MD5对待签名串求签 25 string upperCaseStr = StrToMD5(basestring.ToString()); 26 string result = StrToMD5(upperCaseStr + secret); 27 return result; 28 } 29 30 private static string StrToMD5(string str) 31 { 32 byte[] data = Encoding.UTF8.GetBytes(str); 33 MD5 md5 = new MD5CryptoServiceProvider(); 34 byte[] OutBytes = md5.ComputeHash(data); 35 36 string OutString = ""; 37 for (int i = 0; i < OutBytes.Length; i++) 38 { 39 OutString += OutBytes[i].ToString("x2"); 40 } 41 return OutString.ToUpper(); 42 }