使用 WWW::Mechanize 的代理

Using a proxy with 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;
    }}
$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'], ... );

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

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

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