WWW :: Mechanize和HTTPS代理

时间:2014-05-31 02:55:30

标签: perl www-mechanize

如何告诉WWW::Mechanize使用HTTPS代理?

我有这段代码:

use WWW::Mechanize;

print "WWW::Mechanize: ", $WWW::Mechanize::VERSION, "\n";
print "LWP : ", $LWP::UserAgent::VERSION, "\n";

my $mech = WWW::Mechanize->new();

$mech->proxy( [ 'http', 'https' ], "https://204.228.129.46:8080/" );

$mech->add_handler("request_send",  sub { shift->dump; return });
$mech->add_handler("response_done", sub { shift->dump; return });

$mech->get("http://people.iu.edu/");

这是我的输出:

WWW::Mechanize: 1.73
LWP : 6.06
GET http://people.iu.edu/
Accept-Encoding: gzip
User-Agent: WWW-Mechanize/1.73

(no content)
500 Can't connect to 204.228.129.46:8080
Content-Type: text/plain
Client-Date: Sat, 31 May 2014 02:26:15 GMT
Client-Warning: Internal response

Can't connect to 204.228.129.46:8080\n
LWP::Protocol::https::Socket: SSL connect attempt failed with unknown error SSL wants a read first at c:/Perl/site/lib/LWP/Protocol/http.pm line 41.\n
Error GETing http://people.iu.edu/: Can't connect to 204.228.129.46:8080 

更新 我试过$ENV{...}没有运气的技巧。我的意思是添加这个没有帮助:

BEGIN {

    $ENV{'HTTPS_PROXY'} = 'http://204.228.129.46:8080/';
};

UPDATE2 我在这里和Perlmonks上读过很多线程,遇到同样的问题(包括connection://计划),但没有运气。

最后更新我认为我的代理已损坏,因为我发现有时我的代码有效,有时却没有。

1 个答案:

答案 0 :(得分:2)

第一次尝试应该是正确的,尽管您可能为代理指定了错误的协议。根据您对环境变量的尝试以及您获得的错误消息,我假设您的http和https代理本身会说http,而不是https(通常就是这种情况)。例如。他的正确方法应该是:

$mech->proxy(['http','https],'http://204.228.129.46:8080/');

或者,您可以将http_proxyhttps_proxy环境变量设置为相同的值,并使用$mech->env_proxy使LWP使用它们。

但是,请确保您使用的是最新版本的LWP和LWP :: Protocol :: https。两个版本应至少为6.0.6,因为在这些版本之前,对https的代理支持已被破坏。至少在LWP的情况下这看起来不错,您也可以检查$LWP::Protocol::https::VERSION。在某些Debian / Ubuntu系统上,它可能也适用于版本6.05,因为它们已经在2013年12月包含了必要的补丁。

如果您没有最新版本Net::SSLGlue::LWP,则会对您的版本进行补丁以获得必要的支持。

相关问题