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

查看:119
本文介绍了如何使用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?

推荐答案

由于 HTTP::Message .但这有点隐藏.

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

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

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.

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天全站免登陆