WdtClient.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Web;
  12. namespace WdtSdk
  13. {
  14. class WdtClient
  15. {
  16. public string sid;
  17. public string appkey;
  18. public string appsecret;
  19. public string gatewayUrl;
  20. private Dictionary<string, string> param;
  21. public WdtClient()
  22. {
  23. param = new Dictionary<string, string>();
  24. }
  25. public void putParams(string key, string value)
  26. {
  27. if (key.Length == 0 || value.Length == 0)
  28. throw new Exception("传入参数或者值为空");
  29. param.Add(key, value);
  30. }
  31. public string wdtOpenapi()
  32. {
  33. HttpWebRequest request = null;
  34. HttpWebResponse response = null;
  35. Stream serviceRequestBodyStream = null;
  36. try
  37. {
  38. request = (HttpWebRequest)WebRequest.Create(gatewayUrl);
  39. request.Credentials = CredentialCache.DefaultCredentials;
  40. request.KeepAlive = false;
  41. request.Method = "POST";
  42. request.ContentType = "application/x-www-form-urlencoded";
  43. UTF8Encoding encoding = new UTF8Encoding();
  44. double epoch = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
  45. param.Add("sid", sid);
  46. param.Add("appkey", appkey);
  47. param.Add("timestamp", epoch.ToString("f0"));
  48. string postData = CreateParam(true);
  49. byte[] bodyBytes = encoding.GetBytes(postData);
  50. request.ContentLength = bodyBytes.Length;
  51. using (serviceRequestBodyStream = request.GetRequestStream())
  52. {
  53. serviceRequestBodyStream.Write(bodyBytes, 0, bodyBytes.Length);
  54. serviceRequestBodyStream.Close();
  55. using (response = (HttpWebResponse)request.GetResponse())
  56. {
  57. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  58. {
  59. string result = reader.ReadToEnd();
  60. reader.Close();
  61. return result;
  62. }
  63. }
  64. }
  65. }
  66. catch (Exception ex)
  67. {
  68. //LogManager.WriteError(ex, "Post");
  69. throw;
  70. }
  71. finally
  72. {
  73. if (response != null)
  74. {
  75. response.Close();
  76. }
  77. if (request != null)
  78. {
  79. request.Abort();
  80. }
  81. }
  82. }
  83. private string CreateParam(bool isLower = false)
  84. {
  85. //排序
  86. param = param.OrderBy(r => r.Key).ToDictionary(r => r.Key, r => r.Value);
  87. StringBuilder sb = new StringBuilder();
  88. int i = 0;
  89. foreach (var item in param)
  90. {
  91. if (item.Key == "sign")
  92. continue;
  93. if (i > 0)
  94. {
  95. sb.Append(";");
  96. }
  97. i++;
  98. sb.Append(item.Key.Length.ToString("00"));
  99. sb.Append("-");
  100. sb.Append(item.Key);
  101. sb.Append(":");
  102. sb.Append(item.Value.Length.ToString("0000"));
  103. sb.Append("-");
  104. sb.Append(item.Value);
  105. }
  106. if (isLower)
  107. param.Add("sign", MD5Encrypt(sb + appsecret).ToLower());
  108. else
  109. {
  110. param.Add("sign", MD5Encrypt(sb + appsecret));
  111. }
  112. sb = new StringBuilder();
  113. i = 0;
  114. foreach (var item in param)
  115. {
  116. if (i == 0)
  117. sb.Append(string.Format("{0}={1}", item.Key, HttpUtility.UrlEncode(item.Value, Encoding.UTF8)));
  118. else
  119. sb.Append(string.Format("&{0}={1}", item.Key, HttpUtility.UrlEncode(item.Value, Encoding.UTF8)));
  120. i++;
  121. }
  122. return sb.ToString();
  123. }
  124. private string MD5Encrypt(string strText)
  125. {
  126. MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  127. byte[] result = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strText));
  128. string strMd5 = BitConverter.ToString(result);
  129. strMd5 = strMd5.Replace("-", "");
  130. return strMd5;// System.Text.Encoding.Default.GetString(result);
  131. }
  132. }
  133. public static class Utils
  134. {
  135. public static string ToJsonString(this object obj)
  136. {
  137. if (obj == null)
  138. return null;
  139. return JsonConvert.SerializeObject(obj);
  140. }
  141. public static object ToJsonString(this string json)
  142. {
  143. return json == null ? null : JsonConvert.DeserializeObject(json);
  144. }
  145. }
  146. }