[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Capture WebResponse to MemoryStream?

coconet

4/15/2008 8:26:00 PM

..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?

Thanks.

7 Answers

Peter Duniho

4/15/2008 8:43:00 PM

0

On Tue, 15 Apr 2008 13:26:05 -0700, coconet <coconet@community.nospam>
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

coconet

4/15/2008 8:46:00 PM

0


I think I have one too many streamwriters. Can you sample-code that
out?


On Tue, 15 Apr 2008 13:42:34 -0700, "Peter Duniho"
<NpOeStPeAdM@nnowslpianmk.com> wrote:

>On Tue, 15 Apr 2008 13:26:05 -0700, coconet <coconet@community.nospam>
>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

Peter Duniho

4/15/2008 9:02:00 PM

0

On Tue, 15 Apr 2008 13:46:18 -0700, coconet <coconet@community.nospam> =

wrote:

>
> I think I have one too many streamwriters.

Why do you have any StreamWriters? If you want an exact copy of the =

original stream, just copy the bytes. Don't convert to and from text.

> Can you sample-code that out?

Well, here's a dirt simple approach (assumes you already have a response=
=

stream named "streamInput"):

byte[] rgb =3D new byte[1024];
int cb;
MemoryStream streamOutput =3D new MemoryStream();

while ((cb =3D streamInput.Read(rgb, 0, rgb.Length) > 0)
{
streamOutput.Write(rgb, 0, cb);
}

streamOutput.Position =3D 0;

Assuming you're reading data over the Internet, you might find a buffer =
of =

1500 works slightly better. I picked 1K just out of convenience.

Pete

invisaman75

8/25/2010 2:54:00 AM

0

On Aug 24, 6:52 pm, Formerly Sideways <twoton...@hotmail.com> wrote:
> On Aug 24, 12:57 am, invisaman75 <invisama...@gmail.com> wrote:
>
> > yeah, oh ok right. I guess they don't exist.
>
> I don't doubt that the videos exist.  So what?  Does it really make
> any difference which pickup combination he was or was not using?  I
> mean really?

If doesn't matter why make any comment?

invisaman75

8/25/2010 2:55:00 AM

0

On Aug 24, 7:54 pm, invisaman75 <invisama...@gmail.com> wrote:
> On Aug 24, 6:52 pm, Formerly Sideways <twoton...@hotmail.com> wrote:
>
> > On Aug 24, 12:57 am, invisaman75 <invisama...@gmail.com> wrote:
>
> > > yeah, oh ok right. I guess they don't exist.
>
> > I don't doubt that the videos exist.  So what?  Does it really make
> > any difference which pickup combination he was or was not using?  I
> > mean really?
>
> If doesn't matter why make any comment?

and yes it matters to me!

Formerly Sideways

8/25/2010 1:13:00 PM

0

On Aug 24, 10:55 pm, invisaman75 <invisama...@gmail.com> wrote:
> On Aug 24, 7:54 pm, invisaman75 <invisama...@gmail.com> wrote:
>
> > On Aug 24, 6:52 pm, Formerly Sideways <twoton...@hotmail.com> wrote:
>
> > > On Aug 24, 12:57 am, invisaman75 <invisama...@gmail.com> wrote:
>
> > > > yeah, oh ok right. I guess they don't exist.
>
> > > I don't doubt that the videos exist.  So what?  Does it really make
> > > any difference which pickup combination he was or was not using?  I
> > > mean really?
>
> > If doesn't matter why make any comment?
>
> and yes it matters to me!

But if you aren't into the integrity of Burrell minutiae, why do you
care if someone thinks he used his bridge pickup on occasion? What
difference does that make to anybody? Why are you on a crusade to
clear up that "misunderstanding?"

Maj6th

8/25/2010 2:04:00 PM

0


"Formerly Sideways" <twotone60@hotmail.com> wrote in message
news:99880daf-9725-4dea-9e1a-8d7244b5ebb0@x42g2000yqx.googlegroups.com...
On Aug 24, 10:55 pm, invisaman75 <invisama...@gmail.com> wrote:
> On Aug 24, 7:54 pm, invisaman75 <invisama...@gmail.com> wrote:
>
> > On Aug 24, 6:52 pm, Formerly Sideways <twoton...@hotmail.com> wrote:
>
> > > On Aug 24, 12:57 am, invisaman75 <invisama...@gmail.com> wrote:
>
> > > > yeah, oh ok right. I guess they don't exist.
>
> > > I don't doubt that the videos exist. So what? Does it really make
> > > any difference which pickup combination he was or was not using? I
> > > mean really?
>
> > If doesn't matter why make any comment?
>
> and yes it matters to me!

But if you aren't into the integrity of Burrell minutiae, why do you
care if someone thinks he used his bridge pickup on occasion? What
difference does that make to anybody? Why are you on a crusade to
clear up that "misunderstanding?"



Minutiae are very important, they help us relate to our heroes, and if we
emulate their examples we can capture a part of their genius and play like
them. For example I heard that Herb Ellis would never play a job unless he
was wearing his "lucky" pork chop necklace, after he started wearing this
talisman, it was said that anyone who played with him always remembered the
experience.



Maj6th