Perl:LWP :: UserAgent总是为重定向的URL返回代码200 [英] Perl: LWP::UserAgent returns always code 200 for redirected urls

查看:176
本文介绍了Perl:LWP :: UserAgent总是为重定向的URL返回代码200的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的网址,它可以达到302临时值。移动到另一个页面。

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

如果URL返回代码200(确定)以检索它并且如果返回的是200以外的其他内容,我会尝试访问。

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 $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;

上面的代码ALWAYS返回200,即使它是302.我在Firefox中使用FireBug测试了标题响应。该URL在FireBug的Net模块中返回302 Moved Temporarily。但是perl上面的代码返回200.为什么?

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会自动跟随 HTTP重定向
您可以通过将 max_redirect 选项设置为 0 来禁用此类行为。

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;

这篇关于Perl:LWP :: UserAgent总是为重定向的URL返回代码200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆