3

我想获得最后一个重定向 URL。

喜欢

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

我想在 perl 中通过 url_1 获取 url_2。以前的来源如下。

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;
}

提前致谢。

4

2 回答 2

3

您可以使用以下命令找到导致响应的请求

$response->request()

您可以使用获取链中的先前响应

$response->previous()

全部一起:

while ($response) {
   say $response->request()->uri();
   $response = $response->previous();
}
于 2013-07-02T03:07:43.610 回答
0

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

于 2013-07-02T03:00:41.957 回答