如何使用 LWP::UserAgent 接受 gzip 压缩的内容? [英] How can I accept gzip-compressed content using LWP::UserAgent?

查看:25
本文介绍了如何使用 LWP::UserAgent 接受 gzip 压缩的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Perl 的 LWP::UserAgent 通过 Web 获取一些页面,并希望尽可能地礼貌.默认情况下,LWP::UserAgent 不会通过 gzip 无缝处理压缩内容.有没有一种简单的方法可以做到这一点,为每个人节省一些带宽?

I am fetching some pages over the Web using Perl's LWP::UserAgent and would like to be as polite as possible. By default, LWP::UserAgent does not seamlessly handle compressed content via gzip. Is there an easy way to make it do so, to save everyone some bandwidth?

推荐答案

LWP 内置了此功能,这要归功于 HTTP::Message.不过有点隐蔽.

LWP has this capability built in, thanks to HTTP::Message. But it's a bit hidden.

首先确保你有 Compress::Zlib 安装以便您可以处理 gzip.HTTP::Message::decodable() 将根据您安装的模块输出允许的编码列表;在标量上下文中,此输出采用逗号分隔的字符串形式,您可以将其与 'Accept-Encoding' HTTP 标头一起使用,该标头 LWP 要求您添加到您的 HTTP::Request-s 你自己.(在我的系统上,安装了 Compress::Zlib,列表为gzip, x-gzip, deflate".)

First make sure you have Compress::Zlib installed so you can handle gzip. HTTP::Message::decodable() will output a list of allowed encodings based on the modules you have installed; in scalar context, this output takes the form a comma-delineated string that you can use with the 'Accept-Encoding' HTTP header, which LWP requires you to add to your HTTP::Request-s yourself. (On my system, with Compress::Zlib installed, the list is "gzip, x-gzip, deflate".)

当您的 HTTP::Response 回来时,请务必使用 $response->decoded_content 而不是 $response->content 访问内容.

When your HTTP::Response comes back, be sure to access the content with $response->decoded_content instead of $response->content.

LWP::UserAgent 中,这一切像这样组合在一起:

In LWP::UserAgent, it all comes together like this:

my $ua = LWP::UserAgent->new;
my $can_accept = HTTP::Message::decodable;
my $response = $ua->get('http://stackoverflow.com/feeds', 
    'Accept-Encoding' => $can_accept,
);
print $response->decoded_content;

这也会将文本解码为 Perl 的 unicode 字符串.如果您希望 LWP 解压缩响应,而不是弄乱文本,这样做:

This will also decode text to Perl's unicode strings. If you only want LWP to uncompress the response, and not mess with the text, do like so:

print $response->decoded_content(charset => 'none');

这篇关于如何使用 LWP::UserAgent 接受 gzip 压缩的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆