Get redirected url in perl

2020-04-15 13:07发布

I want to get last of redirect URL.

like

url_1 : http://on.fb.me/4VGeu url_2 : https://www.facebook.com/

I want to get url_2 by url_1 in perl. Previous source is below.

sub get_redirect_location
{
    my ($url) = @_;
    my $ua = LWP::UserAgent->new;

    $ua->proxy('http', 'SAMPLE_PROXY');
    my $req = new HTTP::Request(GET => $url);
    my $res = $ua->request($req);

    return $res->headers_as_string;
}

Thanks in advance.

标签: perl url get
2条回答
Viruses.
2楼-- · 2020-04-15 13:23

You could look at WWW::Mechanize. I have used it before to do something like this. http://search.cpan.org/~jesse/WWW-Mechanize-1.72/lib/WWW/Mechanize.pm#$mech->redirect_ok()

You may also find this post helpful:

Perl WWW::Mechanize (or LWP) get redirect url

查看更多
可以哭但决不认输i
3楼-- · 2020-04-15 13:24

You can find the request that lead to a response using

$response->request()

You can get the previous response in the chain using

$response->previous()

All together:

while ($response) {
   say $response->request()->uri();
   $response = $response->previous();
}
查看更多
登录 后发表回答