Perl: LWP::UserAgent returns always code 200 for redirected urls
-
11-03-2021 - |
Domanda
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?
Soluzione
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;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow