in reply to Re: Re: Re: help with link checking
in thread help with link checking

Randal Schwartz... cool! Your llama book got me going with Perl, and now you're helping me out on Perl Monks. Thanks!

Anyway... I've pasted my final code below and it works. I was just wondering if there was a more efficient way to do it. Right now I am making two requests for the url. I figure this eats up bandwidth and just plain takes longer. Can someone offer a little input? Thanks!

#!/usr/bin/perl use LWP::UserAgent; ($ua = LWP::UserAgent->new)->timeout(20); $url = "http://fly.hiwaay.net/~dbwalker/josie.mp3"; if(($ua->request(HTTP::Request->new('HEAD', $url)))->code() == 200) { $redirect = $ua->simple_request(HTTP::Request->new('HEAD', $url)); if ($redirect->code == 301 or $redirect->code == 302) { print "BAD\n"; } else { print "GOOD\n"; } } else { print "BAD\n"; }

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: help with link checking
by merlyn (Sage) on Mar 07, 2001 at 04:22 UTC
    Just do the one:
    use LWP::UserAgent; use HTTP::Request::Common; my $ua = LWP::UserAgent->new; for my $url (qw(...)) { if ($ua->simple_request(HEAD $url)->code == 200) { # it's good } else { # it's bad } }

    -- Randal L. Schwartz, Perl hacker