问题描述
我是dotnet平台的新手......我想知道asp.net中货币兑换率的代码。可以帮助我吗?
我尝试了什么:
这是网络服务代码..它会抛出错误输入字符串格式不正确.. 。或者建议使用不同的代码。
hi I'm new to dotnet platform...I want to know the code for currency exchange rate in asp.net..can any one help me?
What I have tried:
this is web service code..It throws error input string is not in the correct format...or else suggest the different code.
namespace Currency_Exchange
{
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
[WebMethod]
public string CurrencyConversion(decimal amount, string fromCurrency, string toCurrency)
{
WebClient web = new WebClient();
string url = string.Format("https://www.google.com/finance/converter?fromCurrency={0}&toCurrency={1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);
string response = web.DownloadString(url);
Regex regex = new Regex(@":(?<rhs>.+?),");
string[] arrDigits = regex.Split(response);
string rate = arrDigits[3];
return rate;
}
}
}
推荐答案
string url = string.Format("https://www.google.com/finance/converter?fromCurrency={0}&toCurrency={1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);
你有金额
作为第三个值,但在哪里 {2}
?
You have amount
as third value, but where is {2}
?
string url = string.Format("https://www.google.com/finance/converter?from={0}&to={1}&a={2}", "AED", "ANG", 1);
string response = web.DownloadString(url);
Regex regex = new Regex("<span class=bld>(.*?)</span>");
var result = regex.Match(response).Groups[1].Value;
输出:0.4874 ANG
这是HTML标记来自Google转换器,以防你想知道我如何使用正则表达式。
output: 0.4874 ANG
Here is the HTML markup from Google converter, in case you want to know how I come out with the regular expression.
<div id=currency_converter_result>1 AED = <span class=bld>0.4874 ANG</span>
这里有一些例子:
https://www.google.com/finance/converter?from=SGD&to=SKK&a=1
https:// www。 google.com/finance/converter?from=USD&to=CNY&a=100
以下是新的CurrencyConversion方法。
Here are some example :
https://www.google.com/finance/converter?from=SGD&to=SKK&a=1
https://www.google.com/finance/converter?from=USD&to=CNY&a=100
Here is the new CurrencyConversion method will look like.
public string CurrencyConversion(decimal amount, string fromCurrency, string toCurrency)
{
WebClient web = new WebClient();
string url = string.Format("https://www.google.com/finance/converter?from={0}&to={1}&a={2}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);
string response = web.DownloadString(url);
Regex regex = new Regex("<span class=bld>(.*?)</span>");
var result = regex.Match(response).Groups[1].Value;
return result;
}
<asp:Label ID="lbl_Amount" runat="server" Text="Amount"></asp:Label>
<asp:TextBox ID="txt_amount" runat="server"></asp:TextBox>
<asp:Label ID="lbl_FromCurrency" runat="server" Text="From Currency"></asp:Label>
<asp:TextBox ID="txt_fromCurrency" runat="server"></asp:TextBox>
<asp:Label ID="lbl_toCurrency" runat="server" Text="To Currency"></asp:Label>
<asp:TextBox ID="txt_ToCurrency" runat="server"></asp:TextBox>
<asp:Label ID="lbl_value" runat="server" Text=""></asp:Label>
<asp:Button ID="Btn_Convert" runat="server" Text="Convert" OnClick="Convert"/>
<asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
.cs /代码背后
.cs/code behind
public string CurrencyConversion(decimal amount, string fromCurrency, string toCurrency)
{
WebClient web = new WebClient();
string url = string.Format("https://www.google.com/finance/converter?from={0}&to={1}&a={2}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);
string response = web.DownloadString(url);
Regex regex = new Regex("<span class=bld>(.*?)</span>");
var result = regex.Match(response).Groups[1].Value;
return string.Format("{0} {1} = {2}", amount, fromCurrency, result);
}
protected void Convert(object sender, EventArgs e)
{
lblResult.Text = CurrencyConversion ( decimal.Parse(txt_amount.Text), txt_fromCurrency.Text, txt_ToCurrency.Text);
}
这篇关于如何在ASP.NET中获得货币汇率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!