Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebRequest GetResponseStream read bytes

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();
like image 550
Wowa Avatar asked Dec 13 '11 14:12

Wowa


1 Answers

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.

like image 190
Jon Skeet Avatar answered Oct 22 '22 02:10

Jon Skeet