By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,052 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,052 IT Pros & Developers. It's quick & easy.

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

P: n/a
The following code-snippet works but is just not elegant, IMO.

Does anyone have a recommendation to make this more efficient?

For example, I prefer NOT to have a static "buf" size. In fact, I would
prefer something similar to StreamReader.ReadToEnd() for returning a string,
but into a byte[] array.

Snippet:

HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse();
using (BinaryReader reader = new
BinaryReader(webResponse.GetResponseStream(), enc))
{
byte[] buf = new byte[20000];
int count = reader.Read(buf, 0, buf.Length);
byte[] resp = new byte[count];
for (int i = 0; i < count; i++) resp [i] = buf[i];
}
Oct 2 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

Oct 3 '06 #2

P: n/a
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" <ma**********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
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

Oct 3 '06 #3

P: n/a
Then don't! Use the MemoryStream approach I mentioned... although you should
probably cap the size to some arbitrary value to avoid it trying to allocate
a huge array... it uses a small, disposable buffer when copying between the
streams; no need to allocate your own over-sized array anywhere... Not sure
if you need the encoding / BinaryReader here, since we are reading binary
anyway... I've left it in so as not to break anything...

(snippet not tested, but looks sound)

Marc

HttpWebResponse webResponse =
(HttpWebResponse)webRequest.GetResponse();
byte[] data;
const int BUFFER_SIZE = 2048;
int bytesRead;
byte[] buffer = new byte[BUFFER_SIZE];
using (MemoryStream ms = new MemoryStream())
using (BinaryReader reader = new
BinaryReader(webResponse.GetResponseStream(), enc)) {
while ((bytesRead = reader.Read(buffer, 0, BUFFER_SIZE)) 0) {
ms.Write(buffer, 0, bytesRead);
}
data = ms.ToArray();
}
Oct 3 '06 #4

P: n/a
Thus wrote John,
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.
Note that in .NET 2.0, you can subclass WebClient and override GetWebRequest(),
which gives you full control over the underlying HttpWebRequest.

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
Oct 5 '06 #5

P: n/a
Thinking about it, you could always allocate an array sized using the
content-length header...? If the data is a different length, you probably
have other issues, so throw...?

Marc
Oct 5 '06 #6

P: n/a
Thus wrote Marc,
Thinking about it, you could always allocate an array sized using the
content-length header...?
[...]

Not reliably. There are situations in which there is no Content-Length header.

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
Oct 5 '06 #7

P: n/a
I knew I should have just kept with the mem-stream suggestion. Me and my big
mouth...

Marc
Oct 5 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.