-1

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 "";
    }
}
MLavoie
  • 9,671
  • 41
  • 36
  • 56
Nassif Bousaba
  • 386
  • 1
  • 4
  • 22
  • Well the obvious first thing to do is save `temp` to a file so you can see what you're getting... – Jon Skeet Sep 23 '17 at 07:49
  • @JonSkeet obvious that i've already done that :D as i said im getting the same result without the tag – Nassif Bousaba Sep 23 '17 at 12:28
  • Well it's not really clear what you mean by that. Your question would have been *much* more helpful if you'd said: "When I fetch it with a browser, I get this HTML, and when I fetch it with `WebRequest` I get this HTML" showing both. The code where you look for something that isn't in the string really isn't important - it's the fact that you're not getting the HTML that you expect. – Jon Skeet Sep 23 '17 at 12:38
  • @JonSkeet and the solution for that :P ? – Nassif Bousaba Sep 24 '17 at 06:00
  • Well you still haven't provided a [mcve] (we can't copy/paste/compile/run and it appears to be a WinForms app for no obvious reason) and you haven't provided the HTML in the question. – Jon Skeet Sep 24 '17 at 06:19

1 Answers1

1

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

Z.R.T.
  • 1,543
  • 12
  • 15