16

I need some way to check the size of a download without having to download the entire file. I am using C# and the System.Net.WebClient to do the downloads.The check needs to run in a asp.net webservice.

Thanks

RC1140
  • 8,423
  • 14
  • 48
  • 71

3 Answers3

17

Use HTTP method HEAD to retrieve Content-Length: header.

HEAD / HTTP/1.1
Host: www.example.com

HTTP/1.1 200 OK
Date: Wed, 18 Mar 2009 11:21:51 GMT
Server: Apache/2.2.3 (CentOS)
Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT
ETag: "b80f4-1b6-80bfd280"
Accept-Ranges: bytes
Content-Length: 438 
Connection: close
Content-Type: text/html; charset=UTF-8
vartec
  • 131,205
  • 36
  • 218
  • 244
  • 1
    Hi you can use the code at this page to actually get the details (for anyone that needs the info) http://www.eggheadcafe.com/tutorials/aspnet/2c13cafc-be1c-4dd8-9129-f82f59991517/the-lowly-http-head-reque.aspx – RC1140 Mar 18 '09 at 12:48
  • Assuming that the Content-Length header is there. You'd be surprised how many sites don't supply that information, and how many don't even support HEAD. – Jim Mischel Mar 18 '09 at 15:20
  • What would you use as an alternative – RC1140 Mar 19 '09 at 05:39
  • Actually in these cases there is no alternative. Luckily these are rather rare cases. For example if you download stuff from one of these servers, your browser instead of giving you normal progress bar and ETA, gives you indeterminate progress bar or spinner. – vartec Mar 19 '09 at 12:10
2

Make a HEAD (rather than GET or POST) request to just get the response headers, this should include the content-length header with the information you need.

Richard
  • 106,783
  • 21
  • 203
  • 265
-1

You can also use the HTTP RANGE header to download only the stuff you want.

It would be really simple to build a HttpRangeStream that supports seek and read on a remote HTTP resource, if the remote server is HTTP 1.1 and correctly supports RANGE headers.

Cheeso
  • 189,189
  • 101
  • 473
  • 713