what is better ?
Otis Gospodnetic (otisg@panther.middlebury.edu)
Tue, 23 Jul 1996 17:48:16 -0400 (EDT)
Hello,
I'm wondering what is a better way to check the validity of the URL (and
report the response in case of error):
1)
use LWP::UserAgent;
use URI::URL;
use HTTP::Headers;
use HTTP::Request;
$ua = new LWP::UserAgent;
$header = new HTTP::Headers
'Date' => HTTP::Date::time2str(time), # Convert machine time
# to a format suitable
# for the HTTP server
'Accept' => 'text/html', # Tell the server we can
# accept HTML
'Accept' => 'text/plain', # Tell the server we can
# accept plain text (.txtfiles)
;
$url = new URI::URL("$link");
$request = new HTTP::Request('GET', $url, $header);
$response = $ua->request($request);
if ($response->is_success) {
print $response->content; # Prints the HTML
} else {
print $response->error_as_HTML; # Prints the error as HTML
}
__END__
OR
2)
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$ua->agent("SomeName/1.0");
$req = new HTTP::Request 'GET', "$URL";
$req->header('Accept' => 'text/html');
# 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?
}
__END__
Thanks !
Otis