使用WWW :: Mechanize的代理

时间:2015-05-11 17:52:22

标签: perl www-mechanize

我试图使用多个代理在我的服务器上传功能上执行测试。当我通过api.ipify.org获取我的IP时。控制台输出我的真实IP,而不是代理ip。

use strict; use warnings;
    use WWW::Mechanize;
    my $file = "proxies.txt";
    open (FH, "< $file") or die "Can't open $file for read: $!";
    my @lines = <FH>;
    close FH or die "Cannot close $file: $!";
    print "Loaded proxy list";
    my $m = WWW::Mechanize->new(
        autocheck => 1,
        agent_alias => 'Mozilla',
        cookie_jar => {},
        ssl_opts => {verify_hostname => 0},
        quiet => 0,
    );
    my $httpl = "http://";
    $m->no_proxy('localhost');
    my $ua = LWP::UserAgent->new;
    for(my $i=0; $i < 47; $i++)
    {
    $m->proxy('http', $httpl . '' . $lines[$i]);
    print "Connecting to proxy " . $lines[$i];
    $m->get("https://api.ipify.org?format=json");
    print $m->content;
    for(my $j = 0; $j <= 10; $j++){
    $m->get("http://example.com");
    system("node genran.js");
    $m->post('http://example.com/upload.php',
        Content_Type => "form-data",
        Content => [
            'password' => '',
            'public' => 'yes',
            'uploadContent' => [ 'spam.txt', 'Love pecons', 'Content_$
            file => [ 'x86.png', 'image_name', 'Content-Type' => 'ima$
        ]
    );

    print $m->content;
    }}

1 个答案:

答案 0 :(得分:2)

$m->proxy('http', $httpl . '' . $lines[$i]);
print "Connecting to proxy " . $lines[$i];
$m->get("https://api.ipify.org?format=json");

您只为http设置了代理,但发出了https请求。您需要像这样设置https的代理:

$m->proxy('https', ... put your https proxy here ...);

或者为多个协议使用相同的代理:

$m->proxy(['http','https'], ... );

另外,请确保至少使用版本6.06的LWP :: UserAgent和LWP :: Protocol :: https来支持使用https的代理,即

use LWP::UserAgent;
print LWP::UserAgent->VERSION,"\n";

use LWP::Protocol::https;
print LWP::Protocol::https->VERSION,"\n";