|
@@ -0,0 +1,139 @@
|
|
|
+using System.Web;
|
|
|
+
|
|
|
+namespace OASystem.API.OAMethodLib.JuHeAPI
|
|
|
+{
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static class ExchangeRateTool
|
|
|
+ {
|
|
|
+
|
|
|
+ static string appKey = "0f5429e9fbb8637c0ff3f14bbb42c732";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ static string SendRequest(string url, IDictionary<string, string> parameters, string method)
|
|
|
+ {
|
|
|
+ if (method.ToLower() == "post")
|
|
|
+ {
|
|
|
+ HttpWebRequest req = null;
|
|
|
+ HttpWebResponse rsp = null;
|
|
|
+ System.IO.Stream reqStream = null;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ req = (HttpWebRequest)WebRequest.Create(url);
|
|
|
+ req.Method = method;
|
|
|
+ req.KeepAlive = false;
|
|
|
+ req.ProtocolVersion = HttpVersion.Version10;
|
|
|
+ req.Timeout = 5000;
|
|
|
+ req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
|
|
|
+ byte[] postData = Encoding.UTF8.GetBytes(BuildQuery(parameters, "utf8"));
|
|
|
+ reqStream = req.GetRequestStream();
|
|
|
+ reqStream.Write(postData, 0, postData.Length);
|
|
|
+ rsp = (HttpWebResponse)req.GetResponse();
|
|
|
+ Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
|
|
|
+ return GetResponseAsString(rsp, encoding);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ return ex.Message;
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ if (reqStream != null) reqStream.Close();
|
|
|
+ if (rsp != null) rsp.Close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+
|
|
|
+ HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?" + BuildQuery(parameters, "utf8"));
|
|
|
+
|
|
|
+
|
|
|
+ request.Method = "GET";
|
|
|
+ request.ReadWriteTimeout = 5000;
|
|
|
+ request.ContentType = "text/html;charset=UTF-8";
|
|
|
+ HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
|
+ Stream myResponseStream = response.GetResponseStream();
|
|
|
+ StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
|
|
|
+
|
|
|
+
|
|
|
+ string retString = myStreamReader.ReadToEnd();
|
|
|
+ return retString;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ static string BuildQuery(IDictionary<string, string> parameters, string encode)
|
|
|
+ {
|
|
|
+ StringBuilder postData = new StringBuilder();
|
|
|
+ bool hasParam = false;
|
|
|
+ IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
|
|
|
+ while (dem.MoveNext())
|
|
|
+ {
|
|
|
+ string name = dem.Current.Key;
|
|
|
+ string value = dem.Current.Value;
|
|
|
+
|
|
|
+ if (!string.IsNullOrEmpty(name))
|
|
|
+ {
|
|
|
+ if (hasParam)
|
|
|
+ {
|
|
|
+ postData.Append("&");
|
|
|
+ }
|
|
|
+ postData.Append(name);
|
|
|
+ postData.Append("=");
|
|
|
+ if (encode == "gb2312")
|
|
|
+ {
|
|
|
+ postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312")));
|
|
|
+ }
|
|
|
+ else if (encode == "utf8")
|
|
|
+ {
|
|
|
+ postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ postData.Append(value);
|
|
|
+ }
|
|
|
+ hasParam = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return postData.ToString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
|
|
|
+ {
|
|
|
+ System.IO.Stream stream = null;
|
|
|
+ StreamReader reader = null;
|
|
|
+ try
|
|
|
+ {
|
|
|
+
|
|
|
+ stream = rsp.GetResponseStream();
|
|
|
+ reader = new StreamReader(stream, encoding);
|
|
|
+ return reader.ReadToEnd();
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+
|
|
|
+ if (reader != null) reader.Close();
|
|
|
+ if (stream != null) stream.Close();
|
|
|
+ if (rsp != null) rsp.Close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|