Ms_ Dev

Ms_ Dev

  • NA
  • 236
  • 88.5k

WebClient is failing to execute the url and get the CSV.

Apr 24 2017 6:10 AM
HI,
 
Following code is failing at WebClient statement. I am not able to get the csv from API.
  1. private static async Task<double?> GetStockPriceAsync(string symbol)  
  2.        {  
  3.            if (string.IsNullOrWhiteSpace(symbol))  
  4.                return null;  
  5.   
  6.            string url = $"http://finance.yahoo.com/d/quotes.csv?s={symbol}&f=l1" ;  
  7.            string csv; 
  8.   
  9.            using (WebClient client = new WebClient())  
  10.            {  
  11.                csv = await  client.DownloadStringTaskAsync(url).ConfigureAwait(false);  
  12.            }  
  13.          
  14.            string line = csv.Split('\n')[0];  
  15.            string price = line.Split(',')[1];  
  16.   
  17.            double result;  
  18.            if (double.TryParse(price, out result))  
  19.                return result;  
  20.   
  21.            return null;  
  22.        }  
 help me with the new statement that will work apart from WebClient.
 

Answers (3)