繁体   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