如何强制 LWP 对 HTTPS 请求使用 Crypt::SSLeay? [英] How do I force LWP to use Crypt::SSLeay for HTTPS requests?

查看:18
本文介绍了如何强制 LWP 对 HTTPS 请求使用 Crypt::SSLeay?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的症状是我无法将代理与 HTTPS 请求与 LWP 一起使用.这似乎是一个常见问题,Google 上的提示甚至 here 所有人都建议设置 HTTPS_PROXY 环境变量以供 Crypt::SSLeay 使用.

My symptom is that I cannot use a proxy with HTTPS requests with LWP. This seems to be a common problem, and the hints on Google and even here all suggest a work-around for setting the HTTPS_PROXY environment variable for use by Crypt::SSLeay.

我的具体问题似乎是 LWP::Protocol::https 正在加载 IO::Socket::SSL 而不是 Crypt::SSLeay.如何强制使用 Crypt::SSLeay?

My specific problem appears to be that LWP::Protocol::https is loading IO::Socket::SSL rather than Crypt::SSLeay. How can I force Crypt::SSLeay's use instead?

我的代码:

#!/usr/bin/perl

use strict;
use warnings;
$ENV{HTTPS_PROXY} = 'http://10.0.3.1:3128';
use LWP::UserAgent;
my $ua = LWP::UserAgent->new();
my $req = HTTP::Request->new('GET','https://www.meritrustcu.org/');
my $res = $ua->request($req);
print "$_
" for grep { $_ =~ /SSL/ } keys %INC;

它的输出,表明 Crypt::SSLeay 没有被使用:

And it's output, showing that Crypt::SSLeay is not being used:

Net/SSLeay.pm
IO/Socket/SSL.pm
/usr/lib/perl5/auto/Net/SSLeay/autosplit.ix
/usr/lib/perl5/auto/Net/SSLeay/set_proxy.al
/usr/lib/perl5/auto/Net/SSLeay/randomize.al

简单地向我的脚本添加一个显式的use Crypt::SSLeay 已被证明是无效的.它加载模块,但它继续加载 IO::Socket::SSL,并将其用于 HTTPS 请求.

Simply adding an explicit use Crypt::SSLeay to my script has proven ineffective. It loads the module, but it continues to load IO::Socket::SSL, and use it for the HTTPS requests.

推荐答案

试试这个:

use strict;
use warnings;

use Net::SSL (); # From Crypt-SSLeay
BEGIN {
  $Net::HTTPS::SSL_SOCKET_CLASS = "Net::SSL"; # Force use of Net::SSL
  $ENV{HTTPS_PROXY} = 'http://10.0.3.1:3128';
}

use LWP::UserAgent;
my $ua = LWP::UserAgent->new();
my $req = HTTP::Request->new('GET','https://www.meritrustcu.org/');
my $res = $ua->request($req);
print "$_
" for grep { $_ =~ /SSL/ } keys %INC;

我没有合适的代理,所以我没有自己尝试过.

I don't have a suitable proxy, so I haven't tried it myself.

这篇关于如何强制 LWP 对 HTTPS 请求使用 Crypt::SSLeay?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆