ExchangeRateAPI.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Net;
  6. using System.Text;
  7. using System.Web;
  8. using Newtonsoft.Json;
  9. using Newtonsoft.Json.Linq;
  10. namespace DAL
  11. {
  12. /// <summary>
  13. /// 货币汇率接口
  14. /// </summary>
  15. public class ExchangeRateAPI
  16. {
  17. static string appkey = "0f5429e9fbb8637c0ff3f14bbb42c732"; //配置您申请的appkey
  18. /// <summary>
  19. /// 外汇汇率
  20. /// </summary>
  21. /// <returns></returns>
  22. public string ForeignCurrency()
  23. {
  24. string url = "http://web.juhe.cn:8080/finance/exchange/rmbquot";
  25. var parameters2 = new Dictionary<string, string>();
  26. parameters2.Add("key", appkey);//你申请的key
  27. parameters2.Add("type", "0"); //两种格式(0或者1,默认为0)
  28. parameters2.Add("bank", "3"); //(0:工商银行,1:招商银行,2:建设银行,3:中国银行,4:交通银行,5:农业银行,默认为:0)
  29. string result2 = sendPost(url, parameters2, "get");
  30. JObject jsonText = (JObject)JsonConvert.DeserializeObject(result2);
  31. string errorCode2 = jsonText["error_code"].ToString();
  32. if (errorCode2 == "0")
  33. {
  34. return jsonText["result"].ToString();
  35. }
  36. else
  37. {
  38. // return jsonText["error_code"].ToString() + ":" + jsonText["reason"].ToString();
  39. return "请求超过次数限制!";
  40. }
  41. }
  42. /// <summary>
  43. /// 雷怡 2022-07-29 14;11
  44. /// 外汇汇率
  45. /// </summary>
  46. /// <returns></returns>
  47. public string ForeignCurrency1()
  48. {
  49. string url = "http://web.juhe.cn:8080/finance/exchange/rmbquot";
  50. var parameters2 = new Dictionary<string, string>();
  51. parameters2.Add("key", appkey);//你申请的key
  52. parameters2.Add("type", "0"); //两种格式(0或者1,默认为0)
  53. parameters2.Add("bank", "3"); //(0:工商银行,1:招商银行,2:建设银行,3:中国银行,4:交通银行,5:农业银行,默认为:0)
  54. string result2 = sendPost(url, parameters2, "get");
  55. JObject jsonText = (JObject)JsonConvert.DeserializeObject(result2);
  56. string errorCode2 = jsonText["error_code"].ToString();
  57. if (errorCode2 == "0")
  58. {
  59. return jsonText["result"].ToString();
  60. }
  61. else
  62. {
  63. return jsonText["error_code"].ToString() + ":" + jsonText["reason"].ToString();
  64. }
  65. }
  66. /// <summary>
  67. /// Http (GET/POST)
  68. /// </summary>
  69. /// <param name="url">请求URL</param>
  70. /// <param name="parameters">请求参数</param>
  71. /// <param name="method">请求方法</param>
  72. /// <returns>响应内容</returns>
  73. static string sendPost(string url, IDictionary<string, string> parameters, string method)
  74. {
  75. if (method.ToLower() == "post")
  76. {
  77. HttpWebRequest req = null;
  78. HttpWebResponse rsp = null;
  79. System.IO.Stream reqStream = null;
  80. try
  81. {
  82. req = (HttpWebRequest)WebRequest.Create(url);
  83. req.Method = method;
  84. req.KeepAlive = false;
  85. req.ProtocolVersion = HttpVersion.Version10;
  86. req.Timeout = 5000;
  87. req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
  88. byte[] postData = Encoding.UTF8.GetBytes(BuildQuery(parameters, "utf8"));
  89. reqStream = req.GetRequestStream();
  90. reqStream.Write(postData, 0, postData.Length);
  91. rsp = (HttpWebResponse)req.GetResponse();
  92. Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
  93. return GetResponseAsString(rsp, encoding);
  94. }
  95. catch (Exception ex)
  96. {
  97. return ex.Message;
  98. }
  99. finally
  100. {
  101. if (reqStream != null) reqStream.Close();
  102. if (rsp != null) rsp.Close();
  103. }
  104. }
  105. else
  106. {
  107. //创建请求
  108. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?" + BuildQuery(parameters, "utf8"));
  109. //GET请求
  110. request.Method = "GET";
  111. request.ReadWriteTimeout = 5000;
  112. request.ContentType = "text/html;charset=UTF-8";
  113. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  114. Stream myResponseStream = response.GetResponseStream();
  115. StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
  116. //返回内容
  117. string retString = myStreamReader.ReadToEnd();
  118. return retString;
  119. }
  120. }
  121. /// <summary>
  122. /// 组装普通文本请求参数。
  123. /// </summary>
  124. /// <param name="parameters">Key-Value形式请求参数字典</param>
  125. /// <returns>URL编码后的请求数据</returns>
  126. static string BuildQuery(IDictionary<string, string> parameters, string encode)
  127. {
  128. StringBuilder postData = new StringBuilder();
  129. bool hasParam = false;
  130. IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
  131. while (dem.MoveNext())
  132. {
  133. string name = dem.Current.Key;
  134. string value = dem.Current.Value;
  135. // 忽略参数名或参数值为空的参数
  136. if (!string.IsNullOrEmpty(name))//&& !string.IsNullOrEmpty(value)
  137. {
  138. if (hasParam)
  139. {
  140. postData.Append("&");
  141. }
  142. postData.Append(name);
  143. postData.Append("=");
  144. if (encode == "gb2312")
  145. {
  146. postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312")));
  147. }
  148. else if (encode == "utf8")
  149. {
  150. postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
  151. }
  152. else
  153. {
  154. postData.Append(value);
  155. }
  156. hasParam = true;
  157. }
  158. }
  159. return postData.ToString();
  160. }
  161. /// <summary>
  162. /// 把响应流转换为文本。
  163. /// </summary>
  164. /// <param name="rsp">响应流对象</param>
  165. /// <param name="encoding">编码方式</param>
  166. /// <returns>响应文本</returns>
  167. static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
  168. {
  169. System.IO.Stream stream = null;
  170. StreamReader reader = null;
  171. try
  172. {
  173. // 以字符流的方式读取HTTP响应
  174. stream = rsp.GetResponseStream();
  175. reader = new StreamReader(stream, encoding);
  176. return reader.ReadToEnd();
  177. }
  178. finally
  179. {
  180. // 释放资源
  181. if (reader != null) reader.Close();
  182. if (stream != null) stream.Close();
  183. if (rsp != null) rsp.Close();
  184. }
  185. }
  186. }
  187. }