I'm using google finance to convert a currency to another. The code that I'm using is shown below which was working fine. However, today, I'm facing the IndexOutofrange exception and getting a result of -1 for the indexes searched below (which means that my result does not contain CONVERTED VALUE which is 100% true after logging it.
Then I went to the same web request called, fed it the same parameter, and then inspected the source code from the web browser, and I got the VALUE .
What do you think might be the issue? From a web browser I get the whole result, and from my app the result is missing the converted value field.
private static string CurrencyConvert(decimal amount, string fromCurrency, string toCurrency)
{
try
{
//Grab your values and build your Web Request to the API
string apiURL = String.Format("https://www.google.com/finance/converter?a={0}&from={1}&to={2}&meta={3}", amount, fromCurrency, toCurrency, Guid.NewGuid().ToString());
//Make your Web Request and grab the results
var request = WebRequest.Create(apiURL);
//Get the Response
StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream(), System.Text.Encoding.ASCII);
//Grab your converted value (ie 2.45 USD)
String temp = streamReader.ReadToEnd().ToString();
int pFrom = temp.IndexOf("<span class=bld>") + ("<span class=bld>").Length;
int pTo = temp.LastIndexOf("</span>");
System.Windows.MessageBox.Show(pFrom.ToString() + " " + pTo.ToString());
String result = temp.Substring(pFrom, pTo - pFrom);
// string result = Regex.Matches(streamReader.ReadToEnd(), "<span class=\"?bld\"?>([^<]+)</span>")[0].Groups[1].Value;
//Get the Result
return result;
}
catch(Exception ex )
{
return "";
}
}
problem with URL. use this one:https://finance.google.com/finance/converter?a={0}&from={1}&to={2}&meta={3}
meta parameter unnecessary https://finance.google.com/finance/converter?a={0}&from={1}&to={2} works fine as well