123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Net;
- using System.Text;
- using System.Web;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- namespace DAL
- {
- /// <summary>
- /// 货币汇率接口
- /// </summary>
- public class ExchangeRateAPI
- {
- static string appkey = "0f5429e9fbb8637c0ff3f14bbb42c732"; //配置您申请的appkey
- /// <summary>
- /// 外汇汇率
- /// </summary>
- /// <returns></returns>
- public string ForeignCurrency()
- {
- string url = "http://web.juhe.cn:8080/finance/exchange/rmbquot";
- var parameters2 = new Dictionary<string, string>();
- parameters2.Add("key", appkey);//你申请的key
- parameters2.Add("type", "0"); //两种格式(0或者1,默认为0)
- parameters2.Add("bank", "3"); //(0:工商银行,1:招商银行,2:建设银行,3:中国银行,4:交通银行,5:农业银行,默认为:0)
- string result2 = sendPost(url, parameters2, "get");
- JObject jsonText = (JObject)JsonConvert.DeserializeObject(result2);
- string errorCode2 = jsonText["error_code"].ToString();
- if (errorCode2 == "0")
- {
- return jsonText["result"].ToString();
- }
- else
- {
- // return jsonText["error_code"].ToString() + ":" + jsonText["reason"].ToString();
- return "请求超过次数限制!";
- }
- }
- /// <summary>
- /// 雷怡 2022-07-29 14;11
- /// 外汇汇率
- /// </summary>
- /// <returns></returns>
- public string ForeignCurrency1()
- {
- string url = "http://web.juhe.cn:8080/finance/exchange/rmbquot";
- var parameters2 = new Dictionary<string, string>();
- parameters2.Add("key", appkey);//你申请的key
- parameters2.Add("type", "0"); //两种格式(0或者1,默认为0)
- parameters2.Add("bank", "3"); //(0:工商银行,1:招商银行,2:建设银行,3:中国银行,4:交通银行,5:农业银行,默认为:0)
- string result2 = sendPost(url, parameters2, "get");
- JObject jsonText = (JObject)JsonConvert.DeserializeObject(result2);
- string errorCode2 = jsonText["error_code"].ToString();
- if (errorCode2 == "0")
- {
- return jsonText["result"].ToString();
- }
- else
- {
- return jsonText["error_code"].ToString() + ":" + jsonText["reason"].ToString();
- }
- }
- /// <summary>
- /// Http (GET/POST)
- /// </summary>
- /// <param name="url">请求URL</param>
- /// <param name="parameters">请求参数</param>
- /// <param name="method">请求方法</param>
- /// <returns>响应内容</returns>
- static string sendPost(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"));
- //GET请求
- 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;
- }
- }
- /// <summary>
- /// 组装普通文本请求参数。
- /// </summary>
- /// <param name="parameters">Key-Value形式请求参数字典</param>
- /// <returns>URL编码后的请求数据</returns>
- 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))//&& !string.IsNullOrEmpty(value)
- {
- 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();
- }
- /// <summary>
- /// 把响应流转换为文本。
- /// </summary>
- /// <param name="rsp">响应流对象</param>
- /// <param name="encoding">编码方式</param>
- /// <returns>响应文本</returns>
- static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
- {
- System.IO.Stream stream = null;
- StreamReader reader = null;
- try
- {
- // 以字符流的方式读取HTTP响应
- 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();
- }
- }
- }
- }
|