ExchangeRateTool.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System.Web;
  2. namespace OASystem.API.OAMethodLib.JuHeAPI
  3. {
  4. /// <summary>
  5. /// 集合接口 - 汇率转换tools
  6. /// </summary>
  7. public static class ExchangeRateTool
  8. {
  9. //初始化 appkey
  10. static string appKey = "0f5429e9fbb8637c0ff3f14bbb42c732";
  11. /// <summary>
  12. /// Http (GET/POST)
  13. /// </summary>
  14. /// <param name="url">请求URL</param>
  15. /// <param name="parameters">请求参数</param>
  16. /// <param name="method">请求方法</param>
  17. /// <returns>响应内容</returns>
  18. static string SendRequest(string url, IDictionary<string, string> parameters, string method)
  19. {
  20. if (method.ToLower() == "post")
  21. {
  22. HttpWebRequest req = null;
  23. HttpWebResponse rsp = null;
  24. System.IO.Stream reqStream = null;
  25. try
  26. {
  27. req = (HttpWebRequest)WebRequest.Create(url);
  28. req.Method = method;
  29. req.KeepAlive = false;
  30. req.ProtocolVersion = HttpVersion.Version10;
  31. req.Timeout = 5000;
  32. req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
  33. byte[] postData = Encoding.UTF8.GetBytes(BuildQuery(parameters, "utf8"));
  34. reqStream = req.GetRequestStream();
  35. reqStream.Write(postData, 0, postData.Length);
  36. rsp = (HttpWebResponse)req.GetResponse();
  37. Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
  38. return GetResponseAsString(rsp, encoding);
  39. }
  40. catch (Exception ex)
  41. {
  42. return ex.Message;
  43. }
  44. finally
  45. {
  46. if (reqStream != null) reqStream.Close();
  47. if (rsp != null) rsp.Close();
  48. }
  49. }
  50. else
  51. {
  52. //创建请求
  53. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?" + BuildQuery(parameters, "utf8"));
  54. //GET请求
  55. request.Method = "GET";
  56. request.ReadWriteTimeout = 5000;
  57. request.ContentType = "text/html;charset=UTF-8";
  58. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  59. Stream myResponseStream = response.GetResponseStream();
  60. StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
  61. //返回内容
  62. string retString = myStreamReader.ReadToEnd();
  63. return retString;
  64. }
  65. }
  66. /// <summary>
  67. /// 组装普通文本请求参数。
  68. /// </summary>
  69. /// <param name="parameters">Key-Value形式请求参数字典</param>
  70. /// <returns>URL编码后的请求数据</returns>
  71. static string BuildQuery(IDictionary<string, string> parameters, string encode)
  72. {
  73. StringBuilder postData = new StringBuilder();
  74. bool hasParam = false;
  75. IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
  76. while (dem.MoveNext())
  77. {
  78. string name = dem.Current.Key;
  79. string value = dem.Current.Value;
  80. // 忽略参数名或参数值为空的参数
  81. if (!string.IsNullOrEmpty(name))//&& !string.IsNullOrEmpty(value)
  82. {
  83. if (hasParam)
  84. {
  85. postData.Append("&");
  86. }
  87. postData.Append(name);
  88. postData.Append("=");
  89. if (encode == "gb2312")
  90. {
  91. postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312")));
  92. }
  93. else if (encode == "utf8")
  94. {
  95. postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
  96. }
  97. else
  98. {
  99. postData.Append(value);
  100. }
  101. hasParam = true;
  102. }
  103. }
  104. return postData.ToString();
  105. }
  106. /// <summary>
  107. /// 把响应流转换为文本。
  108. /// </summary>
  109. /// <param name="rsp">响应流对象</param>
  110. /// <param name="encoding">编码方式</param>
  111. /// <returns>响应文本</returns>
  112. static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
  113. {
  114. System.IO.Stream stream = null;
  115. StreamReader reader = null;
  116. try
  117. {
  118. // 以字符流的方式读取HTTP响应
  119. stream = rsp.GetResponseStream();
  120. reader = new StreamReader(stream, encoding);
  121. return reader.ReadToEnd();
  122. }
  123. finally
  124. {
  125. // 释放资源
  126. if (reader != null) reader.Close();
  127. if (stream != null) stream.Close();
  128. if (rsp != null) rsp.Close();
  129. }
  130. }
  131. }
  132. }