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

I thought that code would work, but it doesn't. Check out this simplified version of the script:
#!/usr/bin/perl use LWP::UserAgent; ($ua = LWP::UserAgent->new)->timeout(20); $url = "http://202.103.25.186/music/Phil%20Collins%20-%20That's%20What +%20You%20Said.mp3"; if(($ua->request(HTTP::Request->new('HEAD', $url)))->code() == 200) { print "GOOD\n"; } else { print "BAD\n"; }
When this script is run, it will print GOOD. However, check out http://202.103.25.186/music/Phil%20Collins%20-%20That's%20What%20You%20Said.mp3 in your browser and you'll see that this doesn't serve up an mp3 file, rather an html page. What can I do?

Replies are listed 'Best First'.
Re: Re: Re: help with link checking
by merlyn (Sage) on Mar 07, 2001 at 02:14 UTC
    Ahh, it got a redirect! Use simple_request instead of request and you'll get back the 301/302 code which you can use as a "bad link" confirmation.

    That's what I've done in the various link checkers I've written recently.

    -- Randal L. Schwartz, Perl hacker

      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"; }
        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