Link to home
Start Free TrialLog in
Avatar of drunnels
drunnels

asked on

get file size before downloading via http

In the previous question I was shown how to specify the byte range in an http request (using HTTP::Request and LWP::UserAgent). This allows one to download a specific chunk of a while (e.g., bytes 10000 to 20000).

What I forgot to include in that question is: Can I get the size of the file before specifying the bytes range. Since I will always be retrieving the last x bytes in the file, I would like to get the total size so that I can calculate the range so that the request range is, in effect, total_size minus x to total_size.

thanks
Avatar of Fairlight2cx
Fairlight2cx
Flag of United States of America image

Check the Content-Length response header from code like the following:
use LWP;
my $agent = LWP::UserAgent->new(); 
my $request = HTTP::Request->new(HEAD => "http://members.iglou.com/fairlite/cod4_mapgrabber_setup.exe"); 
my $response = $agent->request($request); 
print($response->as_string);
 
+++++
Response portion pertinent:
 
Content-Length: 3470512

Open in new window

Avatar of drunnels
drunnels

ASKER

I don't think this will work for my purposes. There is a very large file and I need to see the size of it before I request it. I believe that by the time you see the content length in the response, you've already gotten the file.
Negative.  Look carefully at the HTTP::Request->new() call.  We're specifying HEAD, not GET or POST.  That gets -only- the headers, nothing else.
ASKER CERTIFIED SOLUTION
Avatar of Fairlight2cx
Fairlight2cx
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry - missed the HEAD obviously.

thanks