in reply to URL Redirect

Hi Hippo,

Would you subsitute "http://usa.kaspersky.com/files/?file=kis&program=one&lang=en&track=pu_kiskis_usen" for Google and see if there is a way around the extreme timeout? (curl is almost instant.)

Many thanks,
-T

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

    Please can you not start a new subthread when you are actually replying to someone else's post? See how each post has its own "reply" link on the right side? That's what you should be doing.

    The long time is taken on the redirected page. If you're not interested in that then make sure LWP never tries to load it. eg:

    #!/usr/bin/env perl use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new; $ua->requests_redirectable ([]); my $src = 'http://usa.kaspersky.com/files/?file=kis&program=one&lang=e +n&track=pu_kiskis_usen'; my $res = $ua->get($src); if ($res->is_redirect) { print "$src redirects to " . $res->header('Location') . "\n"; } else { print "$src has no redirect. It returned: " . $res->status_line . +"\n"; }

    This returns in a second or two. I once more encourage you to read the documentation for LWP::UserAgent where all of this is explained.

      That did the trick. I was missing
      $ua->requests_redirectable ([]);
      Thank you!

      I do read the docs. I just don't understand what I am seeing, which I why I appreciate your help. (You will notice that I include my references in my code's comments)

      #!/usr/bin/perl use strict; use warnings; #use diagnostics; #use diagnostics "-verbose"; use constant MaxTime1 => 20; # Seconds use LWP::UserAgent; use Term::ANSIColor qw ( BOLD BLUE RED GREEN RESET ); my $Debug = "yes"; my $RedirectUrl; my $IAm = "UrlRedirectTest.pl "; my $WebSite; sub GetUrlRedirect ( $$ ) { # Return $Url's redirected web page. Black string if none if locat +ed # References: # http://stackoverflow.com/questions/26087546/perl-print-the-redi +rected-url # http://search.cpan.org/~ether/libwww-perl-6.15/lib/LWP/UserAgen +t.pm # http://stackoverflow.com/questions/2470053/how-can-i-get-the-ul +timate-url-without-fetching-the-pages-using-perl-and-lwp # https://metacpan.org/pod/HTTP%3A%3AResponse # $r->redirects # Returns the list of redirect responses that lead up to this + response # by following the $r->previous chain. The list order is olde +st first. # In scalar context return the number of redirect responses l +eading up # to this one. # # system ('curl -s "SomeUrl" -w %{redirect_url}'); my $IAm = ( caller(0) )[3]; # Incoming: my $Url = $_[0]; my $Caller = "$_[1]" . "$IAm"; # supposting variables my $ua; my $res; # my $redirect; # my $NewUrl; $ua = LWP::UserAgent->new; $ua->timeout ( MaxTime1 ); $ua->show_progress; $ua->requests_redirectable ([]); $res = $ua->get($Url); # The parentheses around $redirect tell Perl that you want $redirec +t # to be seen as list as you in fact are only interested in the firs +t redirec if ($res->is_redirect) { my ( $NewUrl ) = $res->header('Location'); # my $NewUrl = ( $res->header('Location') ) [1]; if ( "$Debug" eq "yes" ) { print STDERR "Redirect Url = <${NewUrl}>\n"; } return ${NewUrl}; } else { print STDERR "${Caller}: ${Url} has no redirect\n"; return ""; } } $WebSite = 'http://gbis.com'; $RedirectUrl = GetUrlRedirect ( $WebSite, $IAm ); print "WebSite <$WebSite> redirects to <$RedirectUrl>\n\n"; $WebSite = 'http://google.com'; $RedirectUrl = GetUrlRedirect ( $WebSite, $IAm ); print "WebSite <$WebSite> redirects to <$RedirectUrl>\n\n"; $WebSite = 'http://usa.kaspersky.com/files/?file=kis&program=one&lang= +en&track=pu_kiskis_usen'; $RedirectUrl = GetUrlRedirect ( $WebSite, $IAm ); print "WebSite <$WebSite> redirects to <$RedirectUrl>\n\n"; END $ ./Url.Redirest.Test.pl UrlRedirectTest.pl main::GetUrlRedirect: http://gbis.com has no redire +ct WebSite <http://gbis.com> redirects to <> Redirect Url = <http://www.google.com/> WebSite <http://google.com> redirects to <http://www.google.com/> Redirect Url = <https://klamericas.s3.amazonaws.com/files/one/en/kis17 +.0.0.611en_10755.exe> WebSite <http://usa.kaspersky.com/files/?file=kis&program=one&lang=en& +track=pu_kiskis_usen> redirects to <https://klamericas.s3.amazonaws.c +om/files/one/en/kis17.0.0.611en_10755.exe>