开发者

How can I get the HTTP status and Location header in Perl?

开发者 https://www.devze.com 2022-12-17 18:36 出处:网络
I\'m new to Perl but need to use 开发者_Go百科it on a project I\'m working on. What I need to do is check to see if a URL has a 301 redirect and if it has, get the location. The following told me the

I'm new to Perl but need to use 开发者_Go百科it on a project I'm working on. What I need to do is check to see if a URL has a 301 redirect and if it has, get the location. The following told me the code but not the location:

use strict;
use warnings;
require LWP::UserAgent;

my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
$ua->max_redirect(0);

my $response = $ua->get('http://www.actwebdesigns.co.uk/');

if ($response->is_success) {
    print $response->status_line;
    print $response->progress;
}
else {
    die $response->status_line;
}

does anyone know how to get the location?

Regards,

Phil


The $response->header method comes from HTTP::Headers and allows you to inspect the specific headers returned from your request. To find the Location header, use

my $loc = $response->header( 'Location' );


Use the headers contained in HTTP::Response (HTTP::Header), with $response->header.

0

精彩评论

暂无评论...
验证码 换一张
取 消