简体   繁体   English

Perl HTTP ::请求HASH错误?

[英]Perl HTTP::Request HASH error?

I'm having a bit of difficulty with HTTP::Request in Perl, 我在Perl中遇到HTTP :: Request有点困难,

My script is set up like so: 我的脚本设置如下:

use CGI;
use MIME::Base64;
use HTTP::Cookies;
use HTTP::Request;
use LWP::UserAgent;

$request = HTTP::Request->new(GET => 'http://www.example.com/');

 $ua = LWP::UserAgent->new;
 $response = $ua->request($request);

print "$response";

When I run the script on my website, I get a message like so: 当我在我的网站上运行脚本时,我收到如下消息:

HTTP::Response=HASH(0x987f8d8) HTTP ::响应= HASH(0x987f8d8)

(I'm trying to get it to "print" example.com) (我试图让它“打印”example.com)

Yes its a hash. 是的哈希。 You have to access like below. 你必须访问如下。

print $res->decoded_content(); ## if gziped

print $res->content();

print $res->status_line;

Also you can use the Data Dumper to print the whole $res and observe what actually it holds. 您还可以使用数据转储器打印整个$ res并观察它实际存在的内容。

use Data::Dumper;
print Dumper $res;

http::Request returns indeed a hash. http :: Request确实返回一个哈希值。

$response->content has the result. $ response-> content有结果。

From the link: 从链接:

$r->content( $bytes ) This is used to get/set the content and it is inherited from the HTTP::Message base class. $ r-> content($ bytes)这用于获取/设置内容,它继承自HTTP :: Message基类。 See HTTP::Message for details and other methods that can be used to access the content. 有关可用于访问内容的详细信息和其他方法,请参阅HTTP :: Message。 Note that the content should be a string of bytes. 请注意,内容应该是一个字节字符串。 Strings in perl can contain characters outside the range of a byte. perl中的字符串可以包含字节范围之外的字符。 The Encode module can be used to turn such strings into a string of bytes. Encode模块可用于将这些字符串转换为字节串。

You need to do this ... 你需要这样做......

print $response->content();

... take a look at the docs http://metacpan.org/pod/LWP#An-Example ...看看文档http://metacpan.org/pod/LWP#An-Example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM