Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.


In reply to LWP Question by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (4)
    As of 2018-06-23 23:09 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.