1

我尝试使用 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 - 连接建立、代理握手期间出错

请帮助我确定此代码的问题。谢谢!

4

1 回答 1

3

你不能把你的用户名和密码放在 $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

它应该工作!

于 2012-10-01T11:47:56.483 回答