Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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:"); have it give me the redirected url:, 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.


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 = ''; 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?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2016-10-23 02:13 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (299 votes). Check out past polls.