문제

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