Re: Capture WebResponse to MemoryStream?
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Tue, 15 Apr 2008 13:42:34 -0700
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
.
- Follow-Ups:
- Re: Capture WebResponse to MemoryStream?
- From: coconet
- Re: Capture WebResponse to MemoryStream?
- References:
- Capture WebResponse to MemoryStream?
- From: coconet
- Capture WebResponse to MemoryStream?
- Prev by Date: Capture WebResponse to MemoryStream?
- Next by Date: Re: Capture WebResponse to MemoryStream?
- Previous by thread: Capture WebResponse to MemoryStream?
- Next by thread: Re: Capture WebResponse to MemoryStream?
- Index(es):
Relevant Pages
|