1

I had used this code in the last years however google seemed to have changed some of their links. For some reason I am getting this error message:

"Input string was not in a correct format."

in following line:

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

My code:

try
{
    WebClient web = new WebClient();
    string url = string.Format("https://www.google.com/finance/converter?a={2}&from={0}&to={1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);

    string response = web.DownloadString(url);

    Regex regex = new Regex("rhs: \\\"(\\d*.\\d*)");
    Match match = regex.Match(response);
    decimal rate = System.Convert.ToDecimal(match.Groups[1].Value);

    return rate;
}
catch
{
    return 0;
}
croxy
  • 3,966
  • 8
  • 29
  • 43
Mark Fenech
  • 1,338
  • 6
  • 26
  • 36

1 Answers1

7

You may not like this approach but it get's the job done.

WebClient web = new WebClient();
string url = string.Format("https://www.google.com/finance/converter?a={2}&from={0}&to={1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);

string response = web.DownloadString(url);

var split  = response.Split((new string[] { "<span class=bld>"}),StringSplitOptions.None);
var value = split[1].Split(' ')[0];
decimal rate = decimal.Parse(value,CultureInfo.InvariantCulture);
Gericke
  • 1,983
  • 6
  • 41
  • 68