如何实现货币转换
How to implement currency conversion
我已尝试执行以下代码,但它返回了一个空值。
[WebMethod]
public decimal ConvertGOOG(decimal amount, string fromCurrency, string toCurrency)
{
WebClient web = new WebClient();
string url = string.Format("http://www.google.com/ig/calculator?hl=en&q={2}{0}%3D%3F{1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);
string response = web.DownloadString(url);
Regex regex = new Regex("rhs: \\"(\d*.\d*)");
var val = regex.Match(response).Groups[1].Value;
decimal rate = System.Convert.ToDecimal(regex.Match(response).Groups[1].Value);
return rate;
}
从下面的代码中,我得到的 val 值为 null。我在这里尝试将 1 美元兑换成 INR。
我也试过这段代码,但出现 403 forbidden 错误。
var request = WebRequest.Create(apiURL);
//Get the Response
var streamReader = new StreamReader(request.GetResponse().GetResponseStream(), System.Text.Encoding.ASCII);
//Grab your converted value (ie 2.45 USD)
var result = Regex.Matches(streamReader.ReadToEnd(), "<span class=\"?bld\"?>([^<]+)</span>")[0].Groups[1].Value;
我已经尝试GoogleAPI进行代码转换,但它仍然返回空值。
如何将 1 美元兑换成印度卢比?
[WebMethod]
public decimal ConvertGOOG(decimal amount, string fromCurrency, string toCurrency)
{
WebClient web = new WebClient();
string apiURL = String.Format("http://finance.google.com/finance/converter?a={0}&from={1}&to={2}", amount, fromCurrency.ToUpper(), toCurrency.ToUpper());
string response = web.DownloadString(apiURL);
var split = response.Split((new string[] { "<span class=bld>" }), StringSplitOptions.None);
var value = split[1].Split(' ')[0];
decimal rate = decimal.Parse(value, CultureInfo.InvariantCulture);
return rate;
}
这是货币换算的最终方法。 var 值具有转换后的值。
请用以下代码更改您的代码
[WebMethod]
public decimal ConvertGOOG(decimal amount, string fromCurrency, string toCurrency)
{
WebClient web = new WebClient();
string apiURL = String.Format("http://finance.google.com/finance/converter?a={0}&from={1}&to={2}", amount, fromCurrency.ToUpper(), toCurrency.ToUpper());
string response = web.DownloadString(apiURL);
var split = response.Split((new string[] { "<span class=bld>" }), StringSplitOptions.None);
var value = split[1].Split(' ')[0];
decimal rate = decimal.Parse(value, CultureInfo.InvariantCulture);
return rate;
}
这将return转换货币
Google 已停止使用 http://www.google.com/ig/calculator?hl=en&q={2}{0}%3D%3F{1}
我已尝试执行以下代码,但它返回了一个空值。
[WebMethod]
public decimal ConvertGOOG(decimal amount, string fromCurrency, string toCurrency)
{
WebClient web = new WebClient();
string url = string.Format("http://www.google.com/ig/calculator?hl=en&q={2}{0}%3D%3F{1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);
string response = web.DownloadString(url);
Regex regex = new Regex("rhs: \\"(\d*.\d*)");
var val = regex.Match(response).Groups[1].Value;
decimal rate = System.Convert.ToDecimal(regex.Match(response).Groups[1].Value);
return rate;
}
从下面的代码中,我得到的 val 值为 null。我在这里尝试将 1 美元兑换成 INR。
我也试过这段代码,但出现 403 forbidden 错误。
var request = WebRequest.Create(apiURL);
//Get the Response
var streamReader = new StreamReader(request.GetResponse().GetResponseStream(), System.Text.Encoding.ASCII);
//Grab your converted value (ie 2.45 USD)
var result = Regex.Matches(streamReader.ReadToEnd(), "<span class=\"?bld\"?>([^<]+)</span>")[0].Groups[1].Value;
我已经尝试GoogleAPI进行代码转换,但它仍然返回空值。
如何将 1 美元兑换成印度卢比?
[WebMethod]
public decimal ConvertGOOG(decimal amount, string fromCurrency, string toCurrency)
{
WebClient web = new WebClient();
string apiURL = String.Format("http://finance.google.com/finance/converter?a={0}&from={1}&to={2}", amount, fromCurrency.ToUpper(), toCurrency.ToUpper());
string response = web.DownloadString(apiURL);
var split = response.Split((new string[] { "<span class=bld>" }), StringSplitOptions.None);
var value = split[1].Split(' ')[0];
decimal rate = decimal.Parse(value, CultureInfo.InvariantCulture);
return rate;
}
这是货币换算的最终方法。 var 值具有转换后的值。
请用以下代码更改您的代码
[WebMethod]
public decimal ConvertGOOG(decimal amount, string fromCurrency, string toCurrency)
{
WebClient web = new WebClient();
string apiURL = String.Format("http://finance.google.com/finance/converter?a={0}&from={1}&to={2}", amount, fromCurrency.ToUpper(), toCurrency.ToUpper());
string response = web.DownloadString(apiURL);
var split = response.Split((new string[] { "<span class=bld>" }), StringSplitOptions.None);
var value = split[1].Split(' ')[0];
decimal rate = decimal.Parse(value, CultureInfo.InvariantCulture);
return rate;
}
这将return转换货币
Google 已停止使用 http://www.google.com/ig/calculator?hl=en&q={2}{0}%3D%3F{1}