簡體   English   中英

在 LWP Useragent 中使用鏡像 function 時可以解碼 gzip 嗎?

[英]Can I decode gzip when using the mirror function in LWP Useragent?

我可以在 LWP UserAgent 'mirror' 請求中使用 $response->decoded_content 之類的東西嗎? 謝謝你。

使用mirror()時,接收到的數據不會直接添加到響應 object 中,而是直接寫入鏡像文件。 這意味着decoded_content()將不起作用。 但是,您可以添加一個response_header來啟用接收數據的存儲:

use strict;
use warnings;
use LWP::UserAgent ();

my $ua = LWP::UserAgent->new;

my $fn = 'libwww-perl-6.41.tar.gz';  # Example file..
my $url = 'https://cpan.metacpan.org/authors/id/O/OA/OALDERS/'. $fn;
$ua->add_handler(
    response_header => sub {
        my($response, $ua, $handler) = @_;
        $response->{default_add_content} = 1;
    }
);
my $response = $ua->mirror($url, $fn);
if ( $response->is_success ) {
    if ( $response->header('Content-Type') eq 'application/x-gzip') {
        $response->header('Content-Encoding' => 'gzip');
    }
    my $decoded_content = $response->decoded_content;
    # Do someting with the decoded content here ...
}

暫無
暫無

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

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