how to use perl to redirect web page?

P

Paul

hi, there,

I am using perl to access specified web pages. this is my code. when I
run the code, I get the following error: 302 Found. I checked and knew
this is because of redirect.

Would you guys tell me how to solve this problem?

thanks.
paul.


use LWP 5.64;
use LWP::UserAgent;
use HTTP::Headers;
use HTTP::Cookies;


$lurl='http://www.abcddefg.com';
$lbrowser = LWP::UserAgent->new;
$lbrowser->timeout(5);
$lcookie_jar = HTTP::Cookies->new;
$lreq = new HTTP::Request('GET',$lurl);
$lresponse = $lbrowser->simple_request($lreq);
$lcookie_jar->extract_cookies($response);
if ($lresponse->is_success)
{
print "OK. connected with server\n"
}
else
{
print "Error1: when get $lurl:\t" . $lresponse->status_line . "\n";
}
 
J

Jens Thoms Toerring

Paul said:
I am using perl to access specified web pages. this is my code. when I
run the code, I get the following error: 302 Found. I checked and knew
this is because of redirect.
Would you guys tell me how to solve this problem?

"302 Found" isn't an error. It tells you that the page that
you requested temporarily resides somewhere else (for whatever
reasons) and that there's nothing fundamentally wrong. Just go
to the URI you got told about (in the location field of the
response) instead. if you don't believe me go to e.g.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

and look for paragraph 10.3.3. And, by the way, this isn't a
Perl problem but one of how the HTTP protocol works;-)

Regards, Jens
 
J

J. Gleixner

Paul said:
hi, there,

I am using perl to access specified web pages. this is my code. when I
run the code, I get the following error: 302 Found. I checked and knew
this is because of redirect.

Would you guys tell me how to solve this problem?
use LWP::UserAgent;
use HTTP::Headers;
use HTTP::Cookies;

use strict;
$lurl='http://www.abcddefg.com';
$lbrowser = LWP::UserAgent->new;
$lbrowser->timeout(5);
$lcookie_jar = HTTP::Cookies->new;
$lreq = new HTTP::Request('GET',$lurl);
$lresponse = $lbrowser->simple_request($lreq);
[...]

Check the documentation for LWP::UserAgent.

perldoc LWP::UserAgent

You could search for "redirect", to find anything relevant.

e.g.

"The difference from request() is that simple_request() will not try
to handle redirects or authentication responses. "

Always, always, always, start with the documentation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top