perl LWP::UserAgent 给出了一个神秘的错误信息

perl LWP::UserAgent gives a cryptic error message

代码如下:

$vizFile ='https://docs.recipeinvesting.com/t.aaaf.html'; 
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
my $response = $ua->get($vizFile);
if ($response->is_success) {print $response->decoded_content;}
else {print"\nError= $response->status_line]n";}

我收到消息:

Error= HTTP::Response=HASH(0x3a9b810)->status_line]n

如果我把它放在浏览器中,url 可以正常工作。

这一直有效(使用纯 http,使用 LWP::Simple),直到站点进行了一些更改。

会不会是 https 有所作为?

有什么方法可以让错误消息变得不那么隐晦吗?

您不能将代码放在字符串文字中并期望它被执行。当然,您可以放置​​用于插值的变量,但是 making 方法调用落在支持的另一边。

替换

print"\nError= $response->status_line]n";

print "\nError= " . $response->status_line . "\n";

use feature qw( say );

say "\nError= " . $response->status_line;

这将根据需要打印状态行。

请看下面的演示代码,鼓励在代码中包含use strict;use warnings;,这将帮助您避免许多潜在的问题

use strict;
use warnings;
use feature 'say';

use LWP::UserAgent;

my $url ='https://docs.recipeinvesting.com/t.aaaf.html'; 
my $ua  = LWP::UserAgent->new;

$ua->timeout(10);
$ua->env_proxy;

my $response = $ua->get($url);

if( $response->is_success ){
    say $response->decoded_content;
} else {
    die $response->status_line;
}

文档:LWP::UserAgent