为什么 LWP 在 OS X 上失败,但在我的 Windows 10 上没有? 注意:

问题描述

我在 (1) PC/Windows 10 和 perl 5.14.2 和 (2) Mac/OS X 10.11.6 和 perl 5.18.2 上尝试了 getstore() 和 get()。

getstore 产生的一些状态码如下:200 是一个好的 URL,500 是“服务器无法传递”。 有关如何解决此问题的任何想法?

use strict;
use warnings;
use LWP::Simple;

print "\nThe below is successful on both Windows 10 and OS X\n";
my $url = "http://example.com"; 
my $status = getstore($url,"/tmp/web.html"); 
print "Result of getstore for \"$url\"\n is \"$status\" .  Success=" . is_success($status) . "\n";

print "\nThe below is successful on Windows 10 but not on OS X\n";
$url = "https://drive.google.com/open?id=1FqtZptibqeu4uKbv0vzinjWv5bwi8-Y2";
$status = getstore($url,"/tmp/web.html"); 
print "Result of getstore for \"$url\"\n is \"$status\" .  Success=" . is_success($status) . "\n";

print "\nPress Enter to exit";
my $ans =<stdin>;

解决方法

根据 LWP::UserAgent 的文档:

libwww-perl 核心不再为 SSL 捆绑协议插件。你 将需要单独安装 LWP::Protocol::https 以启用支持 用于处理 https-URL。

因此,如@DaveCross 在评论中指出的那样,要访问 https 架构的 url,您需要先安装 LWP::Protocol::https。在 macOS BigSur 上使用 perlbrew perl 版本 5.32.0 进行测试:

$ cpanm LWP::Simple
$ cpanm LWP::Protocol::https
$ perl test.pl  # run your test script
The below is successful on both Windows 10 and OS X
Result of getstore for "http://example.com"
 is "200" .  Success=1

The below is successful on Windows 10 but not on OS X
Result of getstore for "https://drive.google.com/open?id=1FqtZptibqeu4uKbv0vzinjWv5bwi8-Y2"
 is "200" .  Success=1

Press Enter to exit

注意:

它可能在 Windows 上工作的原因是 LWP::Protocol::https 在 Windows 上出现 preinstalled on Strawberry Perl