in reply to URL Redirect

Hi All, Thank you for the suggestions! I have never been able to figure out Net::Perl I should have been more specific. I use LWP::UserAgent a lot and would like to master redirect URL's with it. Problem: it just times out on me. And not the 20 seconds I asked for either. More like several minutes. And "$res->redirects" returns what looks like an reference address pointer I am doing something wrong. cat Url.Redirest.Test.pl

#!/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 # system ('curl -s "SomeUrl" -w %{redirect_url}'); my $IAm = ( caller(0) )[3]; # Incoming: my $Url = $_[0]; my $Caller = "$_[1]" . "$IAm"; my $ua = LWP::UserAgent->new; $ua->timeout ( MaxTime1 ); $ua->show_progress; my $req = new HTTP::Request(GET => $Url); print STDERR "$Caller requesting $Url\n"; my $res = $ua->request($req); print STDERR "$Url redirects to $res->redirects\n"; if ( ! $res->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 = <$res-> +redirects>\n"; } return $res->redirects; } } 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 "WebSite $WebSite\nredirects to\n$RedirectUrl\n"; END

UrlRedirectTest.pl main::GetUrlRedirect requesting http://usa.kaspersky.com/files/?file=kis&program=one&lang=en&track=pu_kiskis_usen http://usa.kaspersky.com/files/?file=kis&program=one&lang=en&track=pu_kiskis_usen redirects to HTTP::Response=HASH(0x24e9808)->redirects RedirectUrl = <HTTP::Response=HASH(0x24e9808)->redirects> 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 09:12 UTC
    and "$res->redirects" returns what looks like an reference address pointer I am doing something wrong

    You are trying to invoke a method call within a string. The method call will not execute and what you see displayed instead is the reference to the object ($res) on which the method would otherwise have been called. You could write it like this:

    print STDERR "$Url redirects to " . $res->redirects . "\n";

    which would be a general solution. However, we can see from the documentation that calling redirects on an HTTP::Response object returns a list, not a scalar. How you treat that will depend upon what you actually want this line of code to display. And what you want is not at all clear to me from the code you have provided.

    Maybe take a step back and explain precisely what it is you are trying to achieve here?