2011-12-08 192 views
6

我有一個簡單的url,它可以做302 temp。移動到另一個頁面。Perl:LWP :: UserAgent總是爲重定向的URL返回代碼200

我嘗試去如果URL返回代碼200(對於確定)來檢索它並停止如果返回200以外的東西。

我的代碼:

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; 

上述代碼總是返回200,即使其302我測試在Firefox使用螢火蟲頭響應。該網址在FireBug的網絡模塊中返回「302 Moved Temporarily」。但是perl中的代碼返回200.爲什麼?

+3

嘗試選項max_redirect => 0.我想它會返回來自上次請求的代碼。 – XoR

回答

17

LWP :: UserAgent自動遵循HTTP redirects。 您可以通過將max_redirect選項設置爲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; 
相關問題