ASP.NET_各个币种之间的汇率转换(实时)使用Yahoo汇率。

  近期开发支付平台的时候有运用到各国的实时汇率之间的转换问题,于是在往上找了很多相关资料,以下就是一些参考网址:

1、提供API接口的网站:https://www.showapi.com;这个网站有提供API的接口,本人换算了下与中国人民银行的汇算率相差不大。1万的换算率相差几块钱。(例:CNY(人民币)-->AUD(澳币));

      该网站提供的汇率换算结果是:1万人民币=1987.5579澳元;

      中国人民银行换算结果是:1万人民币=1980.0100澳元;

(这个换算率是比较准确的,我是比较强烈的推荐使用这个API接口,由于个人只做笔记的问题,暂时还没研究这个API;)

2、谷歌汇率网站:http://www.google.com;这个网站我在测试的时候服务器没有连接通无法进行测试,也不清楚汇率到底是否准确。

3、雅虎汇率网站:http://finance.yahoo.com;这篇博客注重写的是这个网站的API接口,希望对大家开发有帮助,不过雅虎毕竟已经被Verizon收购了。服务器也是自然架设在国外,因此每个国家的汇率可能因此不同。测试的时候测试出汇率跟中国人民银行提供的还是差距蛮大的,因此本来是不想写出来的,但毕竟是花了时间编译 的,还是贴出来跟大家共享下。

 

       该网站提供的汇率换算结果是:1万人民币=1940.00澳元;

 

       中国人民银行换算结果是:1万人民币=1980.0100澳元;

看见了吗?差距1万元就会差了40元,oh my god!这个其实比起跨行转账手续费贵多了。所以是不推荐使用,只推荐学习。

 

准备材料:

1:页面准备:

  1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ccYHOO.aspx.cs" Inherits="ExpressCheckout.ccYHOO" %>
  2 
  3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4 
  5 <html xmlns="http://www.w3.org/1999/xhtml">
  6 <head id="Head1" runat="server">
  7     <title>Realtime Currency Converter ASP.NET Yahoo API</title>
  8     <link rel="stylesheet" href="css/cc.css" />
  9     <script src="JQuery/jquery-1.7.2.min.js" type="text/javascript"></script>
 10     <script src="JQuery/ccYHOO.js" type="text/javascript"></script>
 11 </head>
 12 <body>
 13     <form id="form1" runat="server">
 14     <table class="main">
 15         <tr>
 16             <th colspan="2">
 17                 Realtime Currency Converter ASP.NET Yahoo API
 18             </th>
 19         </tr>
 20         <tr>
 21             <td>
 22                 Convert this amount
 23             </td>
 24             <td>
 25                 <input id="txtAmount" maxlength="12" size="5" value="1" class="controls" />
 26             </td>
 27         </tr>
 28         <tr>
 29             <td>
 30                 From this currency:
 31             </td>
 32             <td>
 33                 <select id="drpFrom" class="controls">
 34                     <option value="AED">United Arab Emirates Dirham (AED)</option>
 35                     <option value="ANG">Netherlands Antillean Guilder (ANG)</option>
 36                     <option value="ARS">Argentine Peso (ARS)</option>
 37                     <option value="AUD">Australian Dollar (AUD)</option>
 38                     <option value="BDT">Bangladeshi Taka (BDT)</option>
 39                     <option value="BGN">Bulgarian Lev (BGN)</option>
 40                     <option value="BHD">Bahraini Dinar (BHD)</option>
 41                     <option value="BND">Brunei Dollar (BND)</option>
 42                     <option value="BOB">Bolivian Boliviano (BOB)</option>
 43                     <option value="BRL">Brazilian Real (BRL)</option>
 44                     <option value="BWP">Botswanan Pula (BWP)</option>
 45                     <option value="CAD">Canadian Dollar (CAD)</option>
 46                     <option value="CHF">Swiss Franc (CHF)</option>
 47                     <option value="CLP">Chilean Peso (CLP)</option>
 48                     <option value="CNY">Chinese Yuan (CNY)</option>
 49                     <option value="COP">Colombian Peso (COP)</option>
 50                     <option value="CRC">Costa Rican Colón (CRC)</option>
 51                     <option value="CZK">Czech Republic Koruna (CZK)</option>
 52                     <option value="DKK">Danish Krone (DKK)</option>
 53                     <option value="DOP">Dominican Peso (DOP)</option>
 54                     <option value="DZD">Algerian Dinar (DZD)</option>
 55                     <option value="EEK">Estonian Kroon (EEK)</option>
 56                     <option value="EGP">Egyptian Pound (EGP)</option>
 57                     <option value="EUR">Euro (EUR)</option>
 58                     <option value="FJD">Fijian Dollar (FJD)</option>
 59                     <option value="GBP">British Pound Sterling (GBP)</option>
 60                     <option value="HKD">Hong Kong Dollar (HKD)</option>
 61                     <option value="HNL">Honduran Lempira (HNL)</option>
 62                     <option value="HRK">Croatian Kuna (HRK)</option>
 63                     <option value="HUF">Hungarian Forint (HUF)</option>
 64                     <option value="IDR">Indonesian Rupiah (IDR)</option>
 65                     <option value="ILS">Israeli New Sheqel (ILS)</option>
 66                     <option value="INR">Indian Rupee (INR)</option>
 67                     <option value="JMD">Jamaican Dollar (JMD)</option>
 68                     <option value="JOD">Jordanian Dinar (JOD)</option>
 69                     <option value="JPY">Japanese Yen (JPY)</option>
 70                     <option value="KES">Kenyan Shilling (KES)</option>
 71                     <option value="KRW">South Korean Won (KRW)</option>
 72                     <option value="KWD">Kuwaiti Dinar (KWD)</option>
 73                     <option value="KYD">Cayman Islands Dollar (KYD)</option>
 74                     <option value="KZT">Kazakhstani Tenge (KZT)</option>
 75                     <option value="LBP">Lebanese Pound (LBP)</option>
 76                     <option value="LKR">Sri Lankan Rupee (LKR)</option>
 77                     <option value="LTL">Lithuanian Litas (LTL)</option>
 78                     <option value="LVL">Latvian Lats (LVL)</option>
 79                     <option value="MAD">Moroccan Dirham (MAD)</option>
 80                     <option value="MDL">Moldovan Leu (MDL)</option>
 81                     <option value="MKD">Macedonian Denar (MKD)</option>
 82                     <option value="MUR">Mauritian Rupee (MUR)</option>
 83                     <option value="MVR">Maldivian Rufiyaa (MVR)</option>
 84                     <option value="MXN">Mexican Peso (MXN)</option>
 85                     <option value="MYR">Malaysian Ringgit (MYR)</option>
 86                     <option value="NAD">Namibian Dollar (NAD)</option>
 87                     <option value="NGN">Nigerian Naira (NGN)</option>
 88                     <option value="NIO">Nicaraguan Córdoba (NIO)</option>
 89                     <option value="NOK">Norwegian Krone (NOK)</option>
 90                     <option value="NPR">Nepalese Rupee (NPR)</option>
 91                     <option value="NZD">New Zealand Dollar (NZD)</option>
 92                     <option value="OMR">Omani Rial (OMR)</option>
 93                     <option value="PEN">Peruvian Nuevo Sol (PEN)</option>
 94                     <option value="PGK">Papua New Guinean Kina (PGK)</option>
 95                     <option value="PHP">Philippine Peso (PHP)</option>
 96                     <option value="PKR">Pakistani Rupee (PKR)</option>
 97                     <option value="PLN">Polish Zloty (PLN)</option>
 98                     <option value="PYG">Paraguayan Guarani (PYG)</option>
 99                     <option value="QAR">Qatari Rial (QAR)</option>
100                     <option value="RON">Romanian Leu (RON)</option>
101                     <option value="RSD">Serbian Dinar (RSD)</option>
102                     <option value="RUB">Russian Ruble (RUB)</option>
103                     <option value="SAR">Saudi Riyal (SAR)</option>
104                     <option value="SCR">Seychellois Rupee (SCR)</option>
105                     <option value="SEK">Swedish Krona (SEK)</option>
106                     <option value="SGD">Singapore Dollar (SGD)</option>
107                     <option value="SKK">Slovak Koruna (SKK)</option>
108                     <option value="SLL">Sierra Leonean Leone (SLL)</option>
109                     <option value="SVC">Salvadoran Colón (SVC)</option>
110                     <option value="THB">Thai Baht (THB)</option>
111                     <option value="TND">Tunisian Dinar (TND)</option>
112                     <option value="TRY">Turkish Lira (TRY)</option>
113                     <option value="TTD">Trinidad and Tobago Dollar (TTD)</option>
114                     <option value="TWD">New Taiwan Dollar (TWD)</option>
115                     <option value="TZS">Tanzanian Shilling (TZS)</option>
116                     <option value="UAH">Ukrainian Hryvnia (UAH)</option>
117                     <option value="UGX">Ugandan Shilling (UGX)</option>
118                     <option value="USD">US Dollar (USD)</option>
119                     <option value="UYU">Uruguayan Peso (UYU)</option>
120                     <option value="UZS">Uzbekistan Som (UZS)</option>
121                     <option value="VEF">Venezuelan Bolívar (VEF)</option>
122                     <option value="VND">Vietnamese Dong (VND)</option>
123                     <option value="XOF">CFA Franc BCEAO (XOF)</option>
124                     <option value="YER">Yemeni Rial (YER)</option>
125                     <option value="ZAR">South African Rand (ZAR)</option>
126                     <option value="ZMK">Zambian Kwacha (ZMK)</option>
127                 </select>
128             </td>
129         </tr>
130         <tr>
131             <td>
132                 To this currency:
133             </td>
134             <td>
135                 <select id="drpTo" class="controls">
136                     <option value="AED">United Arab Emirates Dirham (AED)</option>
137                     <option value="ANG">Netherlands Antillean Guilder (ANG)</option>
138                     <option value="ARS">Argentine Peso (ARS)</option>
139                     <option value="AUD">Australian Dollar (AUD)</option>
140                     <option value="BDT">Bangladeshi Taka (BDT)</option>
141                     <option value="BGN">Bulgarian Lev (BGN)</option>
142                     <option value="BHD">Bahraini Dinar (BHD)</option>
143                     <option value="BND">Brunei Dollar (BND)</option>
144                     <option value="BOB">Bolivian Boliviano (BOB)</option>
145                     <option value="BRL">Brazilian Real (BRL)</option>
146                     <option value="BWP">Botswanan Pula (BWP)</option>
147                     <option value="CAD">Canadian Dollar (CAD)</option>
148                     <option value="CHF">Swiss Franc (CHF)</option>
149                     <option value="CLP">Chilean Peso (CLP)</option>
150                     <option value="CNY">Chinese Yuan (CNY)</option>
151                     <option value="COP">Colombian Peso (COP)</option>
152                     <option value="CRC">Costa Rican Colón (CRC)</option>
153                     <option value="CZK">Czech Republic Koruna (CZK)</option>
154                     <option value="DKK">Danish Krone (DKK)</option>
155                     <option value="DOP">Dominican Peso (DOP)</option>
156                     <option value="DZD">Algerian Dinar (DZD)</option>
157                     <option value="EEK">Estonian Kroon (EEK)</option>
158                     <option value="EGP">Egyptian Pound (EGP)</option>
159                     <option value="EUR">Euro (EUR)</option>
160                     <option value="FJD">Fijian Dollar (FJD)</option>
161                     <option value="GBP">British Pound Sterling (GBP)</option>
162                     <option value="HKD">Hong Kong Dollar (HKD)</option>
163                     <option value="HNL">Honduran Lempira (HNL)</option>
164                     <option value="HRK">Croatian Kuna (HRK)</option>
165                     <option value="HUF">Hungarian Forint (HUF)</option>
166                     <option value="IDR">Indonesian Rupiah (IDR)</option>
167                     <option value="ILS">Israeli New Sheqel (ILS)</option>
168                     <option value="INR">Indian Rupee (INR)</option>
169                     <option value="JMD">Jamaican Dollar (JMD)</option>
170                     <option value="JOD">Jordanian Dinar (JOD)</option>
171                     <option value="JPY">Japanese Yen (JPY)</option>
172                     <option value="KES">Kenyan Shilling (KES)</option>
173                     <option value="KRW">South Korean Won (KRW)</option>
174                     <option value="KWD">Kuwaiti Dinar (KWD)</option>
175                     <option value="KYD">Cayman Islands Dollar (KYD)</option>
176                     <option value="KZT">Kazakhstani Tenge (KZT)</option>
177                     <option value="LBP">Lebanese Pound (LBP)</option>
178                     <option value="LKR">Sri Lankan Rupee (LKR)</option>
179                     <option value="LTL">Lithuanian Litas (LTL)</option>
180                     <option value="LVL">Latvian Lats (LVL)</option>
181                     <option value="MAD">Moroccan Dirham (MAD)</option>
182                     <option value="MDL">Moldovan Leu (MDL)</option>
183                     <option value="MKD">Macedonian Denar (MKD)</option>
184                     <option value="MUR">Mauritian Rupee (MUR)</option>
185                     <option value="MVR">Maldivian Rufiyaa (MVR)</option>
186                     <option value="MXN">Mexican Peso (MXN)</option>
187                     <option value="MYR">Malaysian Ringgit (MYR)</option>
188                     <option value="NAD">Namibian Dollar (NAD)</option>
189                     <option value="NGN">Nigerian Naira (NGN)</option>
190                     <option value="NIO">Nicaraguan Córdoba (NIO)</option>
191                     <option value="NOK">Norwegian Krone (NOK)</option>
192                     <option value="NPR">Nepalese Rupee (NPR)</option>
193                     <option value="NZD">New Zealand Dollar (NZD)</option>
194                     <option value="OMR">Omani Rial (OMR)</option>
195                     <option value="PEN">Peruvian Nuevo Sol (PEN)</option>
196                     <option value="PGK">Papua New Guinean Kina (PGK)</option>
197                     <option value="PHP">Philippine Peso (PHP)</option>
198                     <option value="PKR">Pakistani Rupee (PKR)</option>
199                     <option value="PLN">Polish Zloty (PLN)</option>
200                     <option value="PYG">Paraguayan Guarani (PYG)</option>
201                     <option value="QAR">Qatari Rial (QAR)</option>
202                     <option value="RON">Romanian Leu (RON)</option>
203                     <option value="RSD">Serbian Dinar (RSD)</option>
204                     <option value="RUB">Russian Ruble (RUB)</option>
205                     <option value="SAR">Saudi Riyal (SAR)</option>
206                     <option value="SCR">Seychellois Rupee (SCR)</option>
207                     <option value="SEK">Swedish Krona (SEK)</option>
208                     <option value="SGD">Singapore Dollar (SGD)</option>
209                     <option value="SKK">Slovak Koruna (SKK)</option>
210                     <option value="SLL">Sierra Leonean Leone (SLL)</option>
211                     <option value="SVC">Salvadoran Colón (SVC)</option>
212                     <option value="THB">Thai Baht (THB)</option>
213                     <option value="TND">Tunisian Dinar (TND)</option>
214                     <option value="TRY">Turkish Lira (TRY)</option>
215                     <option value="TTD">Trinidad and Tobago Dollar (TTD)</option>
216                     <option value="TWD">New Taiwan Dollar (TWD)</option>
217                     <option value="TZS">Tanzanian Shilling (TZS)</option>
218                     <option value="UAH">Ukrainian Hryvnia (UAH)</option>
219                     <option value="UGX">Ugandan Shilling (UGX)</option>
220                     <option value="USD">US Dollar (USD)</option>
221                     <option value="UYU">Uruguayan Peso (UYU)</option>
222                     <option value="UZS">Uzbekistan Som (UZS)</option>
223                     <option value="VEF">Venezuelan Bolívar (VEF)</option>
224                     <option value="VND">Vietnamese Dong (VND)</option>
225                     <option value="XOF">CFA Franc BCEAO (XOF)</option>
226                     <option value="YER">Yemeni Rial (YER)</option>
227                     <option value="ZAR">South African Rand (ZAR)</option>
228                     <option value="ZMK">Zambian Kwacha (ZMK)</option>
229                 </select>
230             </td>
231         </tr>
232         <tr>
233             <td colspan="2" class="center">
234                 <input id="submit" type="button" value="Convert" />
235             </td>
236         </tr>
237         <tr class="result">
238             <td colspan="2" id="results" class="center">
239                 Result: ..........
240             </td>
241         </tr>
242     </table>
243     </form>
244 </body>
245 </html>
View Code

2:后台不需要写任何代码,完全是通过JS调用Webservice的;

3:需要添加两个关键的JS文件(ccYHOO.js和jquery-1.7.2.min.js);

4:如果页面需要好看的话,那就多引用一个CSS文件;(css.cs);

这是下载地址: http://files.cnblogs.com/files/BarryJames/JQuery.zip(3-4的文件都在里面可自行下载)。

 

5:准备Webservice:代码如下

 1 using System.Net;
 2 using System.Text.RegularExpressions;
 3 using System.Web.Script.Services;
 4 namespace CurrencyConverter
 5 {
 6     /// <summary>
 7     /// Summary description for WebService
 8     /// </summary>
 9     [WebService(Namespace = "http://tempuri.org/")]
10     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
11     [System.ComponentModel.ToolboxItem(false)]
12     [ScriptService]
13     public class WebService : System.Web.Services.WebService
14     {
15         [WebMethod]
16         public decimal ConvertYHOO(decimal amount, string fromCurrency, string toCurrency)
17         {
18             WebClient web = new WebClient();
19             string url = string.Format("http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={0}{1}=X", fromCurrency.ToUpper(), toCurrency.ToUpper());
20             string response = web.DownloadString(url);
21             string[] values = Regex.Split(response, ",");
22             decimal rate = System.Convert.ToDecimal(values[1]);
23             return rate * amount;
24         }
25     }
26 }
View Code

 

准备工作完成之后就可以开始调试了,调试的页面结果如下:

ASP.NET_各个币种之间的汇率转换(实时)使用Yahoo汇率。_第1张图片

 

你可能感兴趣的:(ASP.NET_各个币种之间的汇率转换(实时)使用Yahoo汇率。)