I have a simple url which does a 302 temp. move to another page.

I try to get to if the URL returns code 200 (for OK) to retrieve it and to stop if something else than 200 is returned.

My code:

my $ua = LWP::UserAgent->new( env_proxy => 1,keep_alive => 1, timeout => 30, agent => "Mozilla/4.76 [en] (Win98; U)");
my $response = $ua->get( $currenturl);
print $response->code;

The code above ALWAYS returns 200, even if its 302. I tested the header response using FireBug in Firefox. The URL returns "302 Moved Temporarily" in the Net module in FireBug. But the code above in perl returns 200. Why?

有帮助吗?

解决方案

LWP::UserAgent automatically follows HTTP redirects. You can disable such behavior by passing max_redirect option set to 0.

my $ua = LWP::UserAgent->new( max_redirect => 0, env_proxy => 1,keep_alive => 1, timeout => 30, agent => "Mozilla/4.76 [en] (Win98; U)");
my $response = $ua->get( $currenturl);
print $response->code;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top