Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

determine url redirection

by sylph001 (Sexton)
on Feb 18, 2015 at 08:43 UTC ( #1117073=perlquestion: print w/replies, xml ) Need Help??

sylph001 has asked for the wisdom of the Perl Monks concerning the following question:

Respective monks,

I need your advices about detecting whether a link is redirected by a web site.

The web site requires my login account and password. I used lwp::useragent and did all the login interactions in my script and successfully got access to the main page as well as the resource link. I can send post request to the resource link directly and get the resource pictures as the response.

It worked for some time however recently I found the link is sometimes redirected to some account setting page. When this happened, the request to the resource link was redirected to a 'account confirm' page. If using web browser, I have to click the 'save' button on this page to be then redirected again to the resource.

Since the web site sometimes does the resource link redirection and sometimes doesn't, I'm considering to let the script deal with both cases. I used $response->is_success() to know if request works, but no idea as to the redirection things.

This is where I'm in huge need of your advices: is there any standard (or best way) to determine whether the resource link is redirected to the 'account' page instead of returning the real resource content? (Here I think we could regard the real resource content as zip files.)

I used $response->is_success() to know if request works, but no idea as to the redirection things.


Best Regards



</body> </html>

Replies are listed 'Best First'.
Re: determine url redirection
by Your Mother (Archbishop) on Feb 18, 2015 at 09:44 UTC

    HTTP::Response also has a ->is_redirect method. Check that and decide what to do.

Re: determine url redirection
by vinoth.ree (Monsignor) on Feb 18, 2015 at 09:04 UTC

    Hi sylph001

    Here is the sample code from one of my project, it prints the redirected URL,

    use LWP::UserAgent; my $UA_Obj = LWP::UserAgent->new; my $request = HTTP::Request->new( GET => 'your_URL_Here' ); my $response = $UA_Obj->request($request); if ( $response->is_success and $response->previous ) { print $request->url, ' redirected to ', $response->request->uri, " +\n"; }
    Update: More on this refer node Get redirected URL

    All is well
Re: determine url redirection
by Anonymous Monk on Feb 18, 2015 at 09:50 UTC

    Usually a redirect results in a different url, which is contained in the response

    Also, WWW::Mechanize keeps a history ... and also tracks a Referrer url

    So just dump the Response to see whats there, you'll find it :)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-03-26 12:44 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (63 votes). Check out past polls.