in reply to URL Redirect

Hi Hippo,

I do not understand from your response why the LWP call takes five minutes to time out, instead of directly giving me the redirect URL

What I am after is achieving with LWP what I can achieve with curl's "-w %{redirect_url}" function. I want to know what the redirect url is

$ curl -s "http://usa.kaspersky.com/files/?file=kis&program=one&lang=e +n&track=pu_kiskis_usen" -w %{redirect_url}

The response from curl is:

https://klamericas.s3.amazonaws.com/files/one/en/kis17.0.0.611en_10755 +.exe

and that response is precisely what I want from LWP.

I have redone my code a bit, but am still getting the same time out and wrong answer back

#!/usr/bin/perl use strict; use warnings; #use diagnostics; #use diagnostics "-verbose"; use constant MaxTime1 => 20; # Seconds # use Net::Nslookup qw ( nslookup ); use LWP::UserAgent; # use LWP::Protocol qw ( https ); use Term::ANSIColor qw ( BOLD BLUE RED GREEN RESET ); my $Debug="yes"; sub GetUrlRedirect ( $$ ) { # Return $Url's redirected web page. Black string if none if locat +ed # Reference: http://stackoverflow.com/questions/26087546/perl-print +-the-redirected-url # http://search.cpan.org/~ether/libwww-perl-6.15/lib/LWP +/UserAgent.pm # http://stackoverflow.com/questions/2470053/how-can-i-g +et-the-ultimate-url-without-fetching-the-pages-using-perl-and-lwp # https://metacpan.org/pod/HTTP%3A%3AResponse # system ('curl -s "SomeUrl" -w %{redirect_url}'); my $IAm = ( caller(0) )[3]; # Incoming: my $Url = $_[0]; my $Caller = "$_[1]" . "$IAm"; my $request; my $result; my $redirect = ""; my $ua = LWP::UserAgent->new; $ua->timeout ( MaxTime1 ); $ua->show_progress; # $ua->requests_redirectable( ); # to read $request = new HTTP::Request(GET => "$Url"); $result = $ua->request($request); $redirect = $result->redirects; print STDERR "result = <$result>\nredirect = <$redirect>\n"; if ( ! $result->is_success ) { print STDERR BOLD RED " ERROR ${Caller}: unable to obtain a redirect web page f +or ", $Url, RESET, "\n"; return ""; } else { if ( "$Debug" eq "yes" ) { print STDERR "RedirectUrl = <$redir +ect>\n"; } return $redirect; } } my $IAm = "UrlRedirectTest.pl "; my $WebSite = 'http://usa.kaspersky.com/files/?file=kis&program=on +e&lang=en&track=pu_kiskis_usen'; my $RedirectUrl = GetUrlRedirect ( $WebSite, $IAm ); print "\nWebSite $WebSite\nredirects to\n$RedirectUrl\n"; END

$ ./Url.Redirest.Test.pl
result = <HTTP::Response=HASH(0x289cb60)>
redirect = <1>
RedirectUrl = <1>

WebSite http://usa.kaspersky.com/files/?file=kis&program=one&lang=en&track=pu_kiskis_usen redirects to
1

Replies are listed 'Best First'.
Re^2: URL Redirect
by hippo (Archbishop) on Sep 06, 2016 at 11:37 UTC

    Here is a simplified script which prints the first redirect of a URL (because in the absense of any statement to the contrary this is what I think you are asking for).

    #!/usr/bin/env perl use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $src = 'http://google.com/'; my $res = $ua->get($src); my ($redirect) = $res->redirects; if (defined $redirect) { print "$src redirects to " . $redirect->header('Location') . "\n"; } else { print "$src has no redirect. It returned: " . $res->status_line . +"\n"; }

    I've used Google as the source because it works whereas your provided source has issues (which you could take up with them). I think that this is pretty clear and shows how the various methods may be used to obtain information of relevance to the programmer. Do read the documentation of the modules you are using.