簡體   English   中英

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 產生了影響?

有什么方法可以獲得不那么神秘的錯誤消息嗎?

您不能將代碼放入字符串文字中並期望它被執行。 當然,您可以放置​​用於插值的變量,但制作方法調用屬於受支持內容的另一側。

代替

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM