2013-02-26 104 views
2

我试图检查重定向页面的标题,并获得302状态, ,但与我的代码,我得到'200 OK'状态的转发页面。 我该怎么做才能让重定向页面302变成staus。如何检查302 http响应

我将不胜感激您的帮助。 谢谢!

我的代码:

use LWP::UserAgent; 

my $ua = LWP::UserAgent->new(); 
my $req = HTTP::Request->new('GET','http://host.com'); 
my $res = $ua->request($req); 
print $res->status_line; 

回答

6

初始化$ UA后,其requests_redirectable属性设置为联合国民主基金:

$ua->requests_redirectable(undef); 

这样,LWP :: UserAgent的不会跟随重定向和第一个请求,而不是停止。

然后使用按年可以得到的代码( “302”, “301” 等):

$res->code() 

这里的official docs for LWP::UserAgent

3

$response->previous()将让你在链之前的响应。

或者如果要禁用重定向,请将requests_redirectable => []传递给LWP :: UserAgent的构造函数。