Hi all,

I have a question involving redirection of URLs. Suppose I am using LWP::UserAgent to send a GET/HEAD request to a URL. But that URL1 may be forwarding to URL2, which may be forwarding to URL3 and so on. So after several redirections we are hitting the final URLn. I want to be able to find out what that final URLn is. Does LWP::UserAgent or any other Perl module keep track of this information? Currently what I am doing is this:

*********************************************************
my $ua = LWP::UserAgent->new; $ua->timeout(5); my $request = HTTP::Request->new(HEAD => $URL1); my $response = $ua->request($request); print $response->base, "\n";
*********************************************************

This works in most cases and returns the final URLn but sometimes it doesn't work. For example, suppose the final URLn is http://cnn.com/technology/internet/google.html

When I call the $response->base method, I am getting back the URL "cnn.com" rather than the full URL as displayed above. So some sites report the base URL to be the top-level domain and not the actual URL containing the data. On the other hand, LWP::UserAgent must clearly know what the full final URL is b/c it has to fetch the data from it. Is that information on the final URLn stored anywhere into the HTTP::Response object or HTTP::Headers object return as part of the usage of LWP::UserAgent?

Any help will be greatly appreciated.
Thanks.

In reply to URL redirection and final URL by haidut

Title:
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, details, 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, summary, 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.