《基于C#的汇率接口调用代码实例》由会员分享,可在线阅读,更多相关《基于C#的汇率接口调用代码实例(6页珍藏版)》请在金锄头文库上搜索。
1、基于 C#的汇率接口调用代码实例代码描述:基于 C#的汇率接口调用代码实例代码平台:聚合数据using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using Xfrog.Net; using System.Diagnostics; using System.Web;/- / 汇率调用示例代码 聚合数据 / 在线接口文档:http:/ / 代码中 JsonObject 类下载地址:http:/ /-namesp
2、ace ConsoleAPI class Programstatic void Main(string args)string appkey = “*“; /配置您申请的 appkey/1.常用汇率查询string url1 = “http:/ parameters1 = new Dictionary();parameters1.Add(“key“, appkey);/你申请的 keystring result1 = sendPost(url1, parameters1, “get“);JsonObject newObj1 = new JsonObject(result1);String er
3、rorCode1 = newObj1“error_code“.Value;if (errorCode1 = “0“)Debug.WriteLine(“成功“);Debug.WriteLine(newObj1);else/Debug.WriteLine(“失败“);Debug.WriteLine(newObj1“error_code“.Value+“:“+newObj1 “reason“.Value);/2.货币列表string url2 = “http:/ parameters2 = new Dictionary();parameters2.Add(“key“, appkey);/你申请的 k
4、eystring result2 = sendPost(url2, parameters2, “get“);JsonObject newObj2 = new JsonObject(result2);String errorCode2 = newObj2“error_code“.Value;if (errorCode2 = “0“)Debug.WriteLine(“成功“);Debug.WriteLine(newObj2);else/Debug.WriteLine(“失败“);Debug.WriteLine(newObj2“error_code“.Value+“:“+newObj2 “reaso
5、n“.Value);/3.实时汇率查询换算string url3 = “http:/ parameters3 = new Dictionary();parameters3.Add(“from“ , “); /转换汇率前的货币代码parameters3.Add(“to“ , “); /转换汇率成的货币代码parameters3.Add(“key“, appkey);/你申请的 keystring result3 = sendPost(url3, parameters3, “get“);JsonObject newObj3 = new JsonObject(result3);String erro
6、rCode3 = newObj3“error_code“.Value;if (errorCode3 = “0“)Debug.WriteLine(“成功“);Debug.WriteLine(newObj3);else/Debug.WriteLine(“失败“);Debug.WriteLine(newObj3“error_code“.Value+“:“+newObj3 “reason“.Value);/ / Http (GET/POST)/ / 请求 URL/ 请求参数/ 请求方法/ 响应内容static string sendPost(string url, IDictionary p aram
7、eters, string method)if (method.ToLower() = “post“)HttpWebRequest req = null;HttpWebResponse rsp = null;System.IO.Stream reqStream = null;tryreq = (HttpWebRequest)WebRequest.Create(url);req.Method = method;req.KeepAlive = false;req.ProtocolVersion = HttpVersion.Version10;req.Timeout = 5000;req.Conte
8、ntType = “application/x-www-form-urlencoded;c harset=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.Ch
9、aract erSet);return GetResponseAsString(rsp, encoding);catch (Exception ex)return ex.Message;finallyif (reqStream != null) reqStream.Close();if (rsp != null) rsp.Close();else/创建请求HttpWebRequest request = (HttpWebRequest)WebRequest.Crea te(url + “?“ + BuildQuery(parameters, “utf8“);/GET 请求request.Met
10、hod = “GET“;request.ReadWriteTimeout = 5000;request.ContentType = “text/html;charset=UTF-8“;HttpWebResponse response = (HttpWebResponse)request.GetR esponse();Stream myResponseStream = response.GetResponseStream();StreamReader myStreamReader = new StreamReader(myRespons eStream, Encoding.GetEncoding
11、(“utf-8“);/返回内容string retString = myStreamReader.ReadToEnd();return retString;/ / 组装普通文本请求参数。/ / Key-Value 形式请求参数字典/ URL 编码后的请求数据static string BuildQuery(IDictionary parameters, string encode)StringBuilder postData = new StringBuilder();bool hasParam = false;IEnumerator dem = parameters.G etEnumerat
12、or();while (dem.MoveNext()string name = dem.Current.Key;string value = dem.Current.Value;/ 忽略参数名或参数值为空的参数if (!string.IsNullOrEmpty(name)/postData.Append(name);postData.Append(“=“);if (encode = “gb2312“)postData.Append(HttpUtility.UrlEncode(value, Enc oding.GetEncoding(“gb2312“);else if (encode = “ut
13、f8“)postData.Append(HttpUtility.UrlEncode(value, Enc oding.UTF8);elsepostData.Append(value);hasParam = true;return postData.ToString();/ / 把响应流转换为文本。/ / 响应流对象/ 编码方式/ 响应文本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();