HttpWebReponse Getbytes Received

  • Thread starter Thread starter Dan McCollick
  • Start date Start date
D

Dan McCollick

Hi all,
I am trying to retreive the number of bytes my app has currently
received from an HTTPWebResponse. I understand that you can retreive
the Content Length in the headers, but isn't this the "Total" bytes.
How do I get the currently received number of bytes? (i.e. I want to
have a progress bar of how long it is taking to receive a stream)

Thank you
Dan McCollick
 
Dan,

The short answer is that you can't.

Reading the headers usually isn't a big deal, and you can ignore showing
some sort of notification for that.

If the ContentLength header is there, then (as you know) you can use
that to show a progress bar as you read the bytes from the content stream.

However, if it is not there, then there is little you can do, as the
response is delimited, not length-based (some messaging formats prefix the
message with the length, not with the HTTP protocol).

In that case, you have to guess.

Hope this helps.
 
Thanks Nicholas,

Further question.

The contentlength header is there, which would give me the "Total"
bytes. How would I show a progress bar using that?
(Clarification: I know how to "show" a progress bar, but where would I
get the received part of the received/total formula?)

Maybe if i rephrase your answer "...you can use that to show a progress
bar as you read the bytes from the content stream".
How do I "read the bytes from the content stream"?

Here is some example code, where would i get the bytes??

HttpWebRequest webRequest3 = WebRequest.Create(TARGET_URL) as
HttpWebRequest;
webRequest3.CookieContainer=cookies;
webRequest3.Method="POST";
webRequest3.ContentType = "application/x-www-form-urlencoded";
webRequest3.AllowAutoRedirect=true;

postData="whatever the postdata happens to be";

StreamWriter requestWriter2 = new
StreamWriter(webRequest3.GetRequestStream());
requestWriter2.Write(postData);

requestWriter2.Close();


responseReader = new
StreamReader(webRequest3.GetResponse().GetResponseStream());
responseData = responseReader.ReadToEnd();
responseReader.Close();
 
Dan,

When you get the response stream through GetResponseStream, that stream
should have a number of bytes equal to the bytes returned in the
ContentLength header (if it is there).

Then, as you read the bytes from the stream, each call to read will
return to you how many bytes are read in that operation. Add it to the
number of bytes that you have read already, and then you can pass that
number to your progress bar (assuming the max is set to the value of the
content length header).

Also, you should be using using statements, like so:

HttpWebRequest webRequest3 = WebRequest.Create(TARGET_URL) as
HttpWebRequest;
webRequest3.CookieContainer=cookies;
webRequest3.Method="POST";
webRequest3.ContentType = "application/x-www-form-urlencoded";
webRequest3.AllowAutoRedirect=true;

postData="whatever the postdata happens to be";

// Use the using statement for auto-dispose semantics.
using (Stream requestStream = webRequest3.GetRequestStream())
using (StreamWriter requestWriter2 = new StreamWriter(requestStream))
{
requestWriter2.Write(postData);
}

// Get the response. This needs to be disposed of as well.
using (HttpWebResponse webResponse = webRequest3.GetResponse() as
HttpWebResponse)
using (Stream responseStream = webResponse.GetResponseStream())
{
// Read the bytes directly from here in a loop, and update your
// progress bar as you do.
}
 
Back
Top