• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

Is it possible to save a remote txt file as a memory stream instead of a file?

i.e. http://www.example.com/example.txt saved direct into a memory stream
  • 2
1 Solution
This is one way.  It's somewhat inefficient because it creates an unnecessary string.  You can look through the response stream and transder a byte at a time to avoid that.  Or you can just use the response stream directly.

            WebRequest wr = HttpWebRequest.Create("http://www.example.com/example.txt");
            WebResponse response = wr.GetResponse();
            TextReader tr = new StreamReader(response.GetResponseStream());
            MemoryStream ms = new MemoryStream();
            StreamWriter w = new StreamWriter(ms);
            string s = tr.ReadToEnd();
stormistAuthor Commented:
drrichards, thank you for your reply. How would I go about using the response stream directly, assuming I wanted to process it line by line?
You can just wrap it in a StreamReader:

    StreamReader rdr = new StreamReader(response.GetResponseStream());
    // Read line by line - in a loop, for example
    string line = rdr.ReadLine();
    // loop until line is null

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now