WebRequest GetResponseStream read bytes

Wowa picture Wowa · Dec 13, 2011 · Viewed 7.9k times · Source

I'm trying to read bytes from an ResponeStream, but how can i say to wait for the data?

If i set a breakpoint after GetResponseStream and wait a few seconds all works fine. Using StreamReader.ReadToEnd() also works fine, but i want to read the bytes myself.

byte[] response = null;

int left = 0;
int steps = 0;
int pos = 0;

int bytelength = 1024;

OnReceiveStart();

using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) {
    using (Stream sr = webResponse.GetResponseStream()) {

        response = new byte[(int)webResponse.ContentLength];

        left = (int)webResponse.ContentLength % bytelength;
        steps = (int)webResponse.ContentLength / bytelength;
        pos = 0;

        for (int i = 0; i < steps; i++) {
            sr.Read(response, pos, bytelength);
            pos += bytelength;
            OnReceiveProgress((int)webResponse.ContentLength, pos);
        }

        if (left != 0) {
            sr.Read(response, pos, left);
        }

        sr.Close();
    }
    webResponse.Close();
}

OnReceiveProgress(1, 1);

OnReceiveFinished();

Answer

Jon Skeet picture Jon Skeet · Dec 13, 2011

Just don't break it into your equal number of steps - instead, just keep reading in a loop until you're done:

while (pos < response.Length)
{
    int bytesRead = sr.Read(response, pos, response.Length - pos);
    if (bytesRead == 0)
    {
        // End of data and we didn't finish reading. Oops.
        throw new IOException("Premature end of data");
    }
    pos += bytesRead;
    OnReceiveProgress(response.Length, pos);
}

Note that you must use the return value of Stream.Read - you can't assume it will read everything you've asked for.