0

我正在尝试向我的 perl 代码添加代理,但我不确定这是否是正确的代码?

#!usr/bin/perl

{

use strict;
 use LWP::UserAgent;
use warnings;
 my $ua = new LWP::UserAgent(agent => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5');
 $ua->proxy([qw(http https)] => 'http://100.100.10.100:80');
 my $response = $ua->get("URL_IN_HERE");
 print $response->code,' ', $response->message,"\n";

}
4

3 回答 3

1

在您的环境中(可能~/.bashrc或这样):

export http_proxy=http://100.100.10.100:80

在 Perl 代码中:

#!/usr/bin/perl

use warnings;
use strict;

use LWP::UserAgent;


my $ua = new LWP::UserAgent(agent => 'Mozilla/5.0');
$ua->env_proxy;
my $response = $ua->get("URL_IN_HERE");
print $response->code,' ', $response->message,"\n";
于 2012-05-24T18:51:09.410 回答
1

请参阅下面的两个示例以了解如何...


如果您的代理127.0.0.1在端口上运行8080并且它支持httpand https,则添加

$ua->proxy(['http', 'https'], 'http://127.0.0.1:8080/');到你的代码。


如果您的代理proxy.mydomain.com在端口上运行80并且它支持httpand ftp,则添加

$ua->proxy(['http', 'ftp'], 'http://proxy.mydomain.com:80/');到你的代码。


于 2012-05-25T02:01:06.497 回答
0

尝试使用下一个代码:)

#!usr/bin/perl
use strict;
use LWP::UserAgent;
use warnings;
{
my $ua = LWP::UserAgent->new;
    $ua->proxy( 'http', "http://127.0.0.1:3128");  #Your proxy
    $ua->default_header("Connection" => "keep-alive");
 $ua->agent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31");
 $ua->timeout (10);
 my $response = $ua->get("http://google.com"); #Website
 print $response->code,' ', $response->message,"\n";

}
于 2017-03-19T16:56:23.963 回答