Perl AnyEvent::HTTP request using Proxy fails

2019-04-13 18:43发布

问题:

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!