in reply to help with link checking

UPDATE : Once again I typed/checked it too slowly...
An please stop voting on this node! upvote the one above...


Replacing
if(($ua->request(HTTP::Request->new('HEAD', $url)))->is_success())
by
if(($ua->request(HTTP::Request->new('HEAD', $url)))->code() == 200)
should work...

Replies are listed 'Best First'.
Re: Re: help with link checking
by rmckillen (Novice) on Mar 07, 2001 at 02:11 UTC
    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?
      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"; }