如何在Perl中使用LWP :: UserAgent获取HTTP响应的主体?

时间:2009-12-17 08:52:46

标签: perl lwp lwp-useragent

我发现LWP::UserAgent->request()的返回包含HTTP响应的标头和正文。我只需要响应的主体来做一些解析,那我该怎么办呢 办?

2 个答案:

答案 0 :(得分:9)

require LWP::UserAgent;

my $ua = LWP::UserAgent->new;

my $response = $ua->get('http://search.cpan.org/');

if ($response->is_success) {

    print $response->decoded_content;  # or whatever
}
else {
    die $response->status_line;
}

response->decoded_content将返回回复正文。

答案 1 :(得分:6)

request方法(according to the manual)返回HTTP::Response对象,该对象具有content方法。只要打电话。

$ua->request->content;