How do you read from a text file on the internet?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JamesGeddes
    New Member
    • Dec 2009
    • 6

    How do you read from a text file on the internet?

    Reading from a text file on your local computer is easy, but surely using a URL to point to a different location should work too?

    The code I have thus far is as follows

    Code:
    using System;
    using System.IO;
    
    namespace csharp_station.howto
    {
        class TextFileReader
        {
            static void Main(string[] args)
            {
                // create reader & open file
                TextReader tr = new StreamReader("http://jamesgeddes.com/wttr.txt");
    
                // read a line of text
                Console.WriteLine(tr.ReadLine());
    
                Console.WriteLine("Enter your name");
                string s7 = Console.ReadLine();
                Console.WriteLine("Hello, {0}!", s7);
                // close the stream
                tr.Close();
            }
        }
    }
    However when this runs the "ArgumentExcept ion was unhandled" box pops up and says "URI formats are not supported."

    All I want to do is read the contents of that text file, use it as the value of a variable or array and display it on the console. Is this possible?

    Thanks everyone!
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I am so far out of my element here it's not funny... but I'm going to assume the streamreader just doesn't understand HTTP communication.

    Using the URL you provided a web browser has no issue with it. Had you considered using a webbrowser control? Let it do all the communication/translation then ask it for the content it obtained?
    System.Windows. Forms.WebBrowse r

    I just dragged on onto a test form, gave it your URI and when run I see the "COMPLETE" text that is in your .txt

    It looks to have a "DocumentComple ted" event... So I would guess that if you respond to this then you would know when you can get your content.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      I am so far out of my element here it's not funny... but I'm going to assume the streamreader just doesn't understand HTTP communication.

      Using the URL you provided a web browser has no issue with it. Had you considered using a webbrowser control? Let it do all the communication/translation then ask it for the content it obtained?
      System.Windows. Forms.WebBrowse r

      I just dragged on onto a test form, gave it your URI and when run I see the "COMPLETE" text that is in your .txt

      It looks to have a "DocumentComple ted" event... So I would guess that if you respond to this then you would know when you can get your content.

      Comment

      Working...