簡體   English   中英

是否可以使用Perl HTTP :: Async模塊讀取標頭?

[英]Is it possible to read headers using Perl HTTP::Async module?

為了優化我的Perl應用程序,我需要使用異步 HTTP請求,因此HTTP響應完成后,我就可以處理其他操作。 因此,我相信我唯一的選擇是使用HTTP :: Async模塊。 這對於簡單的請求來說效果很好,但是我需要從一個響應中捕獲cookie標頭,然后與下一個響應一起發送,因此我需要讀取標頭 我的代碼是:

             ...

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

             ...

但是它不起作用,因為它以錯誤結尾無法對未定義的值調用方法“ header” 顯然$responseundef 我如何在$response獲得undef之前捕獲標頭?

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();
}

應該可以工作,盡管我不確定您根本不需要循環。 沒有完整的程序很難分辨。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM