Re: Is there a clean way to create a byte[] from an HttpWebRequest.Read?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



The thing I don't like is having to preallocate a set buffer size.

OTH, I actually started by using webclient but it turned out that it did not
have enough granularity for setting things like timeout values, etc.

"Marc Gravell" <marc.gravell@xxxxxxxxx> wrote in message
news:1159852050.903599.141200@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Snippet is unreliable; assumes < 20K bytes, and manual copy (instead of
buf.CopyTo() or Buffer.BlockCopy()) is inneficient. Perhaps read (in a
loop, using a smaller buffer) from the response and writing to a
MemoryStream, then call ms.ToArray()? This would work with any size.

In 2.0, you could instead use WebClient:

byte[] data;
using (WebClient wc = new WebClient()) {
data = wc.DownloadData("http://somewhere";);
}

Marc



.