简体   繁体   中英

Perl AnyEvent::HTTP request using Proxy fails

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!

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM