使用Google货币转换API时,输入字符串不是正确的格式错误

时间:2012-04-18 10:30:33

标签: asp.net asp.net-mvc-3

我的应用程序是在用C#.Net编码的Asp.Net MVC3中。 我使用谷歌货币转换API来获得转换。

Google Currency Conversion API 我将从货币到货币值和金额传递给我的控制器。 下面是我的C#代码。

    WebClient web = new WebClient();
    string url = string.Format("http://www.google.com/ig/calculator?hl=en&q={2}{0}%3D%3F{1}", CurrenctCurr.ToUpper(), DestinationCurr.ToUpper(), TotCurrentPay);
    string response = web.DownloadString(url);
    Regex regex = new Regex("rhs: \\\"(\\d*.\\d*)");
    Match match = regex.Match(response);
    decimal rate = Convert.ToDecimal(match.Groups[1].Value);

我在线上收到错误

decimal rate = Convert.ToDecimal(match.Groups[1].Value.ToString());

错误是输入字符串不是正确格式。没有内部异常。

我尝试将其转换为toString()

   decimal rate = Convert.ToDecimal(match.Groups[1].Value.ToString());

此外,我尝试在字符串变量中取匹配的值,然后尝试在其上执行SubString逻辑。但它无法正常工作

string test = match.ToString();
test=test.substring(0,test.Indexof("""));

有一个引号(“),所以即时尝试取字符串的值直到”。 建议我如何解决这个问题。或者我还能在正确的方向上尝试什么。

1 个答案:

答案 0 :(得分:0)

尝试获取网址。

    Uri uri = new Uri(string.Format("http://www.google.com/ig/calculator?hl=en&q={2}{0}%3D%3F{1}", CurrenctCurr.ToUpper(), DestinationCurr.ToUpper(), TotCurrentPay));
    string url = uri.AbsoluteUri + uri.Fragment;
    string response = web.DownloadString(url);