How can I find the final URL after all redirections in Perl?

前端 未结 2 1324
说谎
说谎 2021-01-18 21:19

Lets say I have \"http://www.ritzcarlton.com\" and that redirects me to \"http://www.ritzcarlton.com/en/Default.htm\". Is there a way in Perl to find the end url after all t

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-18 21:43

    Using LWP will follow the redirections for you. You can then interrogate the HTTP::Request object to find out the URI it requested.

    use LWP::UserAgent qw();
    
    my $ua = LWP::UserAgent->new;
    
    my $response = $ua->get('http://www.ritzcarlton.com');
    
    print $response->request->uri . "\n";
    

    Output is:

    http://www.ritzcarlton.com/en/Default.htm

提交回复
热议问题