2012-03-01 36 views
2

要优化我的Perl应用程序我需要使用异步 HTTP请求,所以我可以在HTTP响应完成后处理其他操作。所以我相信我唯一的选择是使用HTTP::Async模块。这对简单的请求工作正常,但我需要从一个响应中捕获cookie头,并将其与下一个响应发送,所以我需要阅读标头。我的代码是:是否可以使用Perl HTTP :: Async模块读取标题?

   ... 

      $async->add($request); 
      while ($response = $async->wait_for_next_response) 
      { 
       threads->yield(); yield(); 
      } 
      $cookie = $response->header('Set-Cookie'); 
      $cookie =~ s/;.*$//; 
      $request->header('Cookie' => $cookie); 

      ... 

,但它不工作,因为它与一个错误结束未定义的值无法调用“头”。显然$responseundef。如何在$response获得undef之前获得标题?

+0

很少有很多异步HTTP模块。您可能想要转向基于事件的模块,如AnyEvent :: HTTP或POE :: Component :: Client :: HTTP,并在回调中处理您的响应。您不应该为您的整个应用程序使用POE或AnyEvent。 – MkV 2012-03-02 01:00:43

回答

4
while ($response = $async->wait_for_next_response) 
{ 
    threads->yield(); yield(); 
} 

保证没有完成,直到$response为假。唯一的假值wait_for_next_response将返回undef。您需要提取循环内的cookie,或缓存循环内的最后一个良好响应。

喜欢的东西

my $last_response; 
while ($response = $async->wait_for_next_response) 
{ 
    $last_response = $response; 
    threads->yield(); yield(); 
} 

应该工作,虽然我不知道你所需要的循环可言。没有完整的程序很难说。

+0

谢谢。我刚刚测试的其他选项是放入循环以下命令:$ cookie = $ response-> header('Set-Cookie')。如果((定义$ response-> header('Set-Cookie'))&&($ response-> header('Set-Cookie')ne''));“*** \ n” – 2012-03-01 20:43:43

相关问题