Filesize using WebClient

  • Thread starter Thread starter Leon Friesema
  • Start date Start date
L

Leon Friesema

Howto get the filesize from a file on "the web" using the 2005
WebClient?

TIA,
Leon
 
Hello, Leon!

LF> Howto get the filesize from a file on "the web" using the 2005
LF> WebClient?

One of the ways will be watching for Content-Length header in ResponseHeaders. But servers often do not set this header.

That is why you cannot obtain valid file size a priori. Web Servers pass contents of the file in the stream-like manner. So you have to receive until server closes connection.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Hello, Leon!

LF> Howto get the filesize from a file on "the web" using the 2005
LF> WebClient?

One of the ways will be watching for Content-Length header in ResponseHeaders. But servers often do not set this header.

That is why you cannot obtain valid file size a priori. Web Servers pass contents of the file in the stream-like manner.
So you have to receive until server closes connection.

Hello Vadym!

Thanks for the answer! The problem is: I only need the last 128 bytes
of the file and (to save time- as well as bandwidth) I wished I did
not have to download the entire stream. Then again, after some
testing: the stream returned by "OpenRead" method does not support
seek operations anyway... Again: thanks for the answer anyway!

Leon
 
LF> Thanks for the answer! The problem is: I only need the last 128 bytes
LF> of the file and (to save time- as well as bandwidth) I wished I did
LF> not have to download the entire stream. Then again, after some
LF> testing: the stream returned by "OpenRead" method does not support
LF> seek operations anyway... Again: thanks for the answer anyway!

Then you can use Range header. Use WebClient.Headers property to add headers

Something like this

WebClient myWebClient = new WebClient();
myWebClient.Headers.Add("Range","bytes=-128");

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
LF> Thanks for the answer! The problem is: I only need the last 128 bytes
LF> of the file and (to save time- as well as bandwidth) I wished I did
LF> not have to download the entire stream. Then again, after some
LF> testing: the stream returned by "OpenRead" method does not support
LF> seek operations anyway... Again: thanks for the answer anyway!

Then you can use Range header. Use WebClient.Headers property to add headers

Something like this

WebClient myWebClient = new WebClient();
myWebClient.Headers.Add("Range","bytes=-128");

Interesting.. I'll look into it!

Leon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top