Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

LWP Question

by Anonymous Monk
on Dec 10, 2012 at 18:34 UTC ( #1008147=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that reads one of my pages to test it. the page it tests, will redirect to a number of different urls.

I use LWP to contact the page and get me the html of the page it loads. Some of them are identical source code.

I need to know what URL it is redirecting to.
How can I get that from LWP?

Here is my code:

my $ua = LWP::UserAgent->new; $ua->agent("Mozilla/8.0"); $ua->cookie_jar(HTTP::Cookies->new(file => "lwpcookies.txt", autosave => 1)); push @{$ua->requests_redirectable}, 'GET'; my $req = HTTP::Request->new(GET => "$_fileLoc"); $req->referer($_fileLoc); # Pass request to the user agent and get a response back $res = $ua->request($req); print $res->status_line, "\n"; if($res->status_line =~ /404 Not Found/i) { $_bad++; } else { if($res->is_success) { print $res->is_success, "\n"; if($res->status_line =~ /404 Not Found/i) { $_bad++; } else { $_cn = $res->content; print $_cn, "\n"; } } else { $_bad1++; $_errorT = $res->status_line; print qq~error: "~ . $_errorT . '"'. \t;#' } }
that is a section of my code.
How can I get this to only tell me the URL it redirected to? not the url it ended up on, but where it redirected to, like this: header("Location: http://mydomain.com/path1/someotherpath.php"); have it give me the redirected url: http://mydomain.com/path1/someotherpath.php, because someotherpath.php would also redirect to one other location, so I need the first one.

Can someone tell me how to do this?

Thank you.

Richard

Comment on LWP Question
Download Code
Replies are listed 'Best First'.
Re: LWP Question
by zentara (Archbishop) on Dec 10, 2012 at 22:58 UTC
Re: LWP Question
by Khen1950fx (Canon) on Dec 11, 2012 at 07:46 UTC
    You might find this useful:
    #!/usr/bin/perl -l use strict; use warnings; use IO::Handle; use LWP::UserAgent; my $url = 'http://bit.ly/gf4hl'; my $ua = LWP::UserAgent->new( requests_redirectable => [], ); $ua->show_progress(1); my $res = $ua->get($url); print $res->request->uri->as_string; print $res->header('location');

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1008147]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2015-07-31 00:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls