Perl AnyEvent::HTTP request using Proxy fails

 ̄綄美尐妖づ 提交于 2019-12-21 22:18:19

问题


I tried using the perl module AnyEvent::HTTP to make asynchronous HTTP requests by following this post: http://www.windley.com/archives/2012/03/asynchronous_http_requests_in_perl_using_anyevent.shtml

However, I'm not able to get it working through proxies...

    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;    

I get the following error for the above code (after substituting with authentication details for the proxy)

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

Without the proxy argument of the request, it works. From the CPAN page http://metacpan.org/pod/AnyEvent::HTTP,

595 - errors during connection establishment, proxy handshake

Please help me identify the problem with this code. Thanks!


回答1:


You cant put your username and password in the $host itself. You need to first encode them in base64 by hand and add the resulting string in the Proxy-Authorization header.

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

Then add this line to your header:

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

It should work then!



来源:https://stackoverflow.com/questions/12671099/perl-anyeventhttp-request-using-proxy-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!