已解决:我通过安装 HTML::HeadParser 修复了它。但我仍然不知道为什么它突然停止工作。
我注意到一些以前有效的 LWP 请求停止工作,所以我制作了一个小脚本来检查原因。出于某种原因,我没有从大多数网站获得任何内容(目前仅有的两个有效的是 icanhazip.com 和 gdata.youtube.com api)。我得到一个状态 200 并且我得到了标题的正确 Content-Lenght。我已经尝试在 curl 和 wget 中毫无问题地获取页面。如果我使用 tcpdump 检查请求,我确实会得到包含所有正确信息的回复。我已经尝试使用 LWP::UserAgent 和 LWP::Simple 获得相同的结果。
#!/usr/bin/perl
use warnings;
use strict;
use LWP::UserAgent;
my $agent = LWP::UserAgent->new;
$agent->agent("Mozilla/5.0");
my $req = HTTP::Request->new(GET => 'http://stackoverflow.com');
my $res = $agent->request($req);
print $res->status_line, "\n";
print $res->header("Content-Length"), "\n";
print $res->content, "\n";
使用 DumpLex 转储 $res 会得到以下结果:
$HTTP_Response1 = bless( {
_content => '',
_headers => bless( {
"cache-control"
=> 'public, max-age=15',
"client-aborted"
=> 'die',
"client-date"
=> 'Sat, 23 Jun 2012 15:07:23 GMT',
"client-peer"
=> '64.34.119.12:80',
"client-response-num"
=> 1,
connection => 'close',
"content-length"
=> 211684,
"content-type"
=> 'text/html; charset=utf-8',
date => 'Sat, 23 Jun 2012 15:07:22 ',
expires => 'Sat, 23 Jun 2012 15:07:38 ',
"last-modified"
=> 'Sat, 23 Jun 2012 15:06:38 GMT ',
vary => '*',
"x-died" => 'Can\'t locate HTML/HeadParser.pm in @INC (@INC contains: /usr/lib/perl5/site_perl /usr/share/perl5/site_perl /usr/lib/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib/perl5/core_perl /usr/share/perl5/core_perl .) at /usr/share/perl5/site_perl/LWP/UserAgent.pm line 663.'
}, 'HTTP::Headers' ),
_msg => 'OK',
_protocol
=> 'HTTP/1.1',
_rc => 200,
_request => bless( {
_content => '',
_headers => bless( {
"content-type" => 'application/html',
"user-agent" => 'Mozilla/5.0'
}, 'HTTP::Headers' ),
_method => 'GET',
_uri => \do { my $v = 'http://stackoverflow.com/' },
_uri_canonical
=> 'V: $HTTP_Response1->{_request}{_uri}'
}, 'HTTP::Request' ),
default_add_content
=> 1
}, 'HTTP::Response' );
$HTTP_Response1->{_request}{_uri_canonical} = $HTTP_Response1->{_request}{_uri};
bless( $HTTP_Response1->{_request}{_uri}, 'URI::http' );