Re: what is better ?
Slava Kozlov (slava@glow.myriadagency.com)
Tue, 23 Jul 1996 20:57:03 -0400 (EDT)
On Tue, 23 Jul 1996, Otis Gospodnetic wrote:
> Hello,
>
> I'm wondering what is a better way to check the validity of the URL (and
> report the response in case of error):
>
>
> 2)
>
> use LWP::UserAgent;
Add this:
use URI::URL;
>
> $ua = new LWP::UserAgent;
> $ua->agent("SomeName/1.0");
Before you go out on the net, make sure you've got a good
url:
unless (defined ($URL)) {
'Bad URL Syntax';
return 0;
}
>
> $req = new HTTP::Request 'GET', "$URL";
If you're only 'checking' links, don't download the entire
html page. Use, 'HEAD':
$request = new HTTP::Request('HEAD', $url, $header);
> $req->header('Accept' => 'text/html');
>
Add whatever Accepts and Date and other headers.
> # send request
> $res = $ua->request($req);
>
> # check the outcome
> if ($res->is_success) {
> return 1;
> } else {
> ########## what do I print here to get spit the err to the browser?
The code's in $res->code, and the standard messages are in
HTTP::Status:status_message:
print HTTP::Status::status_message($res->code);
return 0;
> }
> __END__
>
Also you should check for Redirection ($res->code = 301, Moved Permanently
and $res->code = 302, Moved Temporarily) and start the whole checking thing
again.
>
> Thanks !
>
> Otis
>
>
-slava
slava kozlov, myriadagency, 212 334 3132