Monday, April 25, 2011

Checking Download size before download

Hi

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

From stackoverflow
  • 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.

  • 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
    
    RC1140 : 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
    Jim Mischel : 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.
    RC1140 : What would you use as an alternative
    vartec : 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.
  • 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.

0 comments:

Post a Comment