Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

determine url redirection

by sylph001 (Sexton)
on Feb 18, 2015 at 08:43 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-03-15 05:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found