Perl: LWP::UserAgent returns always code 200 for redirected urls

↘锁芯ラ 提交于 2019-12-08 16:33:40

问题


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?


回答1:


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;


来源:https://stackoverflow.com/questions/8432375/perl-lwpuseragent-returns-always-code-200-for-redirected-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!