使用代理的Perl AnyEvent :: HTTP请求失败

时间:2012-10-01 09:57:52

标签: perl http asynchronous proxy anyevent

我尝试使用perl模块AnyEvent :: HTTP通过以下帖子发出异步HTTP请求:http://www.windley.com/archives/2012/03/asynchronous_http_requests_in_perl_using_anyevent.shtml

但是,我无法通过代理工作......

    my $request;
    my $host = '<userid>:<pswd>@<proxyip>';
    my $port = '<proxyport>';
    my $headers = { ...                        
                    'Accept-Language'=> 'en-us,en;q=0.5',
                    'Accept-Charset'=> 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
                    'Keep-Alive' => 300,
                    };

    $request = http_request(
      GET => $url,
      timeout => 120, # seconds
      headers => $headers,
      proxy => [$host, $port],
      mymethod
   );

$cv->end;
$cv->recv;    

上面的代码出现以下错误(在用代理的身份验证详细信息替换之后)

   { 
      'Reason' => 'No such device or address',
      'URL' => 'www.google.com',
      'Status' => 595
    };

没有请求的代理参数,它可以工作。来自CPAN页面http://metacpan.org/pod/AnyEvent::HTTP

595 - 连接建立期间的错误,代理握手

请帮我识别此代码的问题。谢谢!

1 个答案:

答案 0 :(得分:4)

您无法将您的用户名和密码放在$ host本身。您需要先手动在base64中对它们进行编码,然后在Proxy-Authorization标头中添加结果字符串。

$ echo -n user:pass | openssl enc -a
dXNlcjpwYXNz

然后将此行添加到标题中:

my $request;
my $host = '<proxyip>'; #EDITED 
my $port = '<proxyport>';
my $headers = { ...                        
                'Accept-Language'=> 'en-us,en;q=0.5',
                'Accept-Charset'=> 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
                'Keep-Alive' => 300,
                'Proxy-Authorization'=>'Basic dXNlcjpwYXNz' #EDITED
                };

$request = http_request(
  GET => $url,
  timeout => 120, # seconds
  headers => $headers,
  proxy => [$host, $port],
  mymethod

它应该工作了!