Any way I can know the length of the response stream so I can show the user a percentage progress of download instead of an indeterminate one?
I find that the Response looks like below, non seekable and with no length
If you call the GetRequestStream method, you must use the GetResponse method to retrieve the response. If a WebException is thrown, use the Response and Status properties of the exception to determine the response from the server.
The HttpWebRequest class provides support for the properties and methods defined in WebRequest and for additional properties and methods that enable the user to interact directly with servers using HTTP.
Try using the WebResponse.ContentLength
property. If returns -1, it means that the remote server is not sending you the length of the response body, and therefore you cannot determine the length of the response without reading it in its entirety.
If you are interested, I have a code reference library that includes a WebHelper class. It's similar to the WebClient class (including the ability to report progress), but is more flexible. It is stored on CodePlex, the project name is BizArk.
It uses the Response.ContentLength property to determine the length of the response.
The ContentLength property is simply sent as part of the header from the server. There are situations where the server may not know the length of the response prior to sending the header. For example, if you are downloading a dynamically generated webpage with the buffering turned off on the server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With