我希望使用
LWP::UserAgent
从 noaa.gov
服务器获取 JSON 文件。
这是我正在使用的脚本:
#!/usr/bin/perl
use strict;
use warnings;
no warnings 'uninitialized';
my ($ua, $res);
use LWP::UserAgent;
$ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
$res = $ua->get("https://services.swpc.noaa.gov/products/alerts.json");
# specify a CA path
$ua = LWP::UserAgent->new(
ssl_opts => {
SSL_ca_path => '/etc/ssl/certs',
verify_hostname => 1,
}
);
print "Content-type: text/html\n\n";
print "Alert<br>\n";
print $res;
print "<br>/n";
print $ua;
exit;
从我的服务器访问脚本会给出以下内容:
Alert
HTTP::Response=HASH(0x8395e1c)
/nLWP::UserAgent=HASH(0x8445048)
我该去哪里?
根据 LWP::UserAgent POD 对于
get
:
返回值是一个响应对象。
POD 还显示了
decoded_content
方法的用法:
use strict;
use warnings;
no warnings 'uninitialized';
my ($ua, $res);
use LWP::UserAgent;
$ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
$res = $ua->get("https://services.swpc.noaa.gov/products/alerts.json");
# specify a CA path
$ua = LWP::UserAgent->new(
ssl_opts => {
SSL_ca_path => '/etc/ssl/certs',
verify_hostname => 1,
}
);
if ($res->is_success) {
print $res->decoded_content;
}
else {
die $res->status_line;
}