3

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.

hoshipeace
  • 51
  • 6

2 Answers2

3

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();
}
ikegami
  • 367,544
  • 15
  • 269
  • 518
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

Community
  • 1
  • 1
braden.groom
  • 305
  • 2
  • 5
  • 16