Re: Capture WebResponse to MemoryStream?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Tue, 15 Apr 2008 13:26:05 -0700, coconet <coconet@xxxxxxxxxxxxxxxx> wrote:

.NET 3.5
I have an HttpWebResponse.GetReponseStream() that I iterate over. Well
now it looks like I'll have to iterate over it twice. And that type of
stream does not support Seek() back to the beginning.

So I'd like to copy it to a MemoryStream and instead iterate over that
two times. Can anyone show how that'd be possible?

It's as simple as creating a new MemoryStream instance, reading data from your response Stream, writing each chunk of data as you read it into the MemoryStream, until you reach the end of the response Stream.

Assuming the stream is reasonably short, this should be fine. You could in fact write out to the MemoryStream as you're processing the response Stream the first time, and then only read the MemoryStream one more time. This could be slightly more efficient, and would allow your first iteration of the stream to happen immediately rather than being delayed until the original stream has fully completed. But otherwise, it's probably reasonable to just make your initial processing of the original stream create the MemoryStream only, and then iterate that stream twice.

For longer data, you're not going to want to use MemoryStream. In that case, you might consider getting a temporary filename from the Path class (Path.GetTempFileName), and using it for a FileStream instead. Same basic idea applies though.

Pete
.



Relevant Pages

  • Re: Capture WebResponse to MemoryStream?
    ... I have an HttpWebResponse.GetReponseStreamthat I iterate over. ... stream does not support Seekback to the beginning. ... It's as simple as creating a new MemoryStream instance, ... until you reach the end of the response Stream. ...
    (microsoft.public.dotnet.framework)
  • Re: Enumerators and generators
    ... Generators can be used to create a "controlled stream" of objects, ... Enumerator offers methods to iterate through the entire stream, ... I've used generators, but enumerators are newer to me. ...
    (comp.lang.ruby)
  • Re: Why are "broken iterators" broken?
    ... I have an object which can iterate over its parts... ... Then even the stream analogy holds, you don't expect a stream to say ... first interpreted the phrase as being regardless of a fresh call to ...
    (comp.lang.python)
  • Capture WebResponse to MemoryStream?
    ... I have an HttpWebResponse.GetReponseStreamthat I iterate over. ... now it looks like I'll have to iterate over it twice. ... stream does not support Seekback to the beginning. ... So I'd like to copy it to a MemoryStream and instead iterate over that ...
    (microsoft.public.dotnet.framework)
  • Re: serialize a file which is stored in a memorystream object
    ... stream can indeed be serialised and deserialised by the binary formatter. ... msgData, Int32 type) ... I have a property that returns a memorystream that has had a file loaded ... BinaryFormatter objSerializer = new BinaryFormatter; ...
    (microsoft.public.dotnet.languages.csharp)