0

I am trying to read data from excel sheet from a URL (ex:- https://example.com). This URL will download an excel sheet to local machine but my requirement is to read data without downloading the file to local machine. I tried using StreamReader but getting

System.Net.WebException (the connection was aborted) error at reader.ReadToEnd().

Here is my code:

public static void GetStream()
{
    String strURL = String.Empty;
    strURL = "https://example.com";    
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);    
    StreamReader reader;    
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        reader = new StreamReader(response.GetResponseStream());
    }
    var en = reader.CurrentEncoding; //System.Text.UTF8Encoding    
    string val = reader.ReadToEnd();               
}
rene
  • 41,474
  • 78
  • 114
  • 152
  • If anything the `reader.ReadToEnd();` call needs to be within the using block. – rene Dec 19 '17 at 15:54
  • Thanks Rene. Now error is not coming up but the data is not converted to string. reader.CurrentEncoding; is giving me "System.Text.UTF8Encoding". – TestingApp Dec 19 '17 at 16:00
  • Well, no idea why what is behind https://example.com won't give you content. Use a browser to visit https://example.com/ and see if that works. – rene Dec 19 '17 at 16:03
  • example.com/filename is an excel file having 2 rows and multiple columns. I need to read those rows(using this sheet data to compare). – TestingApp Dec 19 '17 at 16:08
  • Sure, use EPPlus or CloseXML – rene Dec 19 '17 at 16:10

0 Answers0