[英]How can I accept gzip-compressed content using LWP::UserAgent?
I am fetching some pages over the Web using Perl's LWP::UserAgent
and would like to be as polite as possible. 我使用Perl的LWP::UserAgent
通过Web获取一些页面,并希望尽可能礼貌。 By default, LWP::UserAgent
does not seamlessly handle compressed content via gzip. 默认情况下, LWP::UserAgent
不能通过gzip无缝处理压缩内容。 Is there an easy way to make it do so, to save everyone some bandwidth? 是否有一种简单的方法可以实现这一目标,为每个人节省一些带宽?
LWP has this capability built in, thanks to HTTP::Message
. 由于HTTP::Message
,LWP内置了这种功能。 But it's a bit hidden. 但它有点隐藏。
First make sure you have Compress::Zlib
installed so you can handle gzip
. 首先确保安装了Compress::Zlib
以便处理gzip
。 HTTP::Message::decodable()
will output a list of allowed encodings based on the modules you have installed; HTTP::Message::decodable()
将根据您安装的模块输出允许编码列表; 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. 在标量上下文中,此输出采用逗号描述的字符串形式,您可以将其与“ Accept-Encoding
”HTTP标头一起使用, LWP
要求您自己将其添加到HTTP::Request
-s。 (On my system, with Compress::Zlib
installed, the list is " gzip
, x-gzip
, deflate
".) (在我的系统上,安装了Compress::Zlib
,列表是“ gzip
, x-gzip
, deflate
”。)
When your HTTP::Response
comes back, be sure to access the content with $response->decoded_content
instead of $response->content
. 当您的HTTP::Response
返回时,请务必使用$response->decoded_content
而不是$response->content
。
In LWP::UserAgent
, it all comes together like this: 在LWP::UserAgent
,这一切都像这样:
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;
This will also decode text to Perl's unicode strings. 这也将解码文本到Perl的unicode字符串。 If you only want LWP
to uncompress the response, and not mess with the text, do like so: 如果您只希望LWP
解压缩响应,而不是弄乱文本,请执行以下操作:
print $response->decoded_content(charset => 'none');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.