Re: Checking

Clinton Wong (clintdw@netcom.com)
Wed, 10 Sep 1997 21:44:34 -0700 (PDT)


Benjamin Hill says:
> I'd like to be able to check if a URL exists, without downloading it.  
> I'm making a script that automatically checks if rather large files are 
> where they should be.
> 
> As things are now, I have to download them to see if they are there, and 
> with the sizes, things get ugly.
> 
> $rawdata = get 'http://wherever/bigfile.gz';
> if (defined $rawdata) print "Yay!";
> 
> Is there a way to just check, withouth the long delay and the bandwidth 
> crunching?

Try this:

#!/usr/bin/perl
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;

my $ua = new LWP::UserAgent;
my $request = new HTTP::Request('HEAD', $url);
my $response = $ua->request($request);

if ($response->is_success) {
  print "Yay!\n";
} else {
  print "Nay!\n";
}

I haven't actually tried this to see if it works, but you get the idea.
Basically, it uses HEAD instead of GET to just get HTTP headers
instead of the whole response (headers + entity body).

For more details, check out the HTTP spec at http://www.w3.org and
the man pages for the various LWP modules.  Or for people in a hurry,
there's also the Web Client Programming book by O'Reilly.

Cheers,
Clinton