LWP请求中没有内容

时间:2012-06-23 11:10:42

标签: perl lwp

已解决:我通过安装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' );

2 个答案:

答案 0 :(得分:3)

在这样的情况下,当一个模块似乎在其记录的功能之外行为不端时,强制重新安装

会很有用。
cpan -f -i LWP::UserAgent

我建议你现在就这样做,以防你的模块库出现其他问题。

答案 1 :(得分:1)

你的问题中的脚本工作得很好。我打赌你有一些库的问题。

我建议你运行

perl -MCPAN -e 'install LWP::UserAgent'

确保您的图书馆是最新的。