How to read a file from internet?

I think the WebClient-class is appropriate for that:  

WebClient client = new WebClient();
Stream stream = client.OpenRead("http://yoururl/test.txt");
StreamReader reader = new StreamReader(stream);
String content = reader.ReadToEnd();

http://msdn.microsoft.com/en-us/library/system.net.webclient.openread.aspx


from http://www.csharp-station.com/HowTo/HttpWebFetch.aspx

    HttpWebRequest  request  = (HttpWebRequest)
        WebRequest.Create("myurl");

        // execute the request
        HttpWebResponse response = (HttpWebResponse)
            request.GetResponse();
            // we will read data via the response stream
        Stream resStream = response.GetResponseStream();
    string tempString = null;
    int    count      = 0;

    do
    {
        // fill the buffer with data
        count = resStream.Read(buf, 0, buf.Length);

        // make sure we read some data
        if (count != 0)
        {
            // translate from bytes to ASCII text
            tempString = Encoding.ASCII.GetString(buf, 0, count);

            // continue building the string
            sb.Append(tempString);
        }
    }
    while (count > 0); // any more data to read?

    // print out page source
    Console.WriteLine(sb.ToString());

A little bit easier way:

string fileContent = new WebClient().DownloadString("yourURL");

Tags:

C#

File