2012-10-01 61 views
2

我使用Perl模块AnyEvent :: HTTP按照这个帖子进行异步HTTP请求的尝试:http://www.windley.com/archives/2012/03/asynchronous_http_requests_in_perl_using_anyevent.shtmlPerl的AnyEvent :: HTTP请求失败

不过,我没有能够得到它通过代理工作...

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 
    }; 

没有代理ARG(与代理认证细节代之后)它的请求,它的作品。从CPAN页http://metacpan.org/pod/AnyEvent::HTTP

595 - 连接建立过程中出现错误,代理握手

请帮我找出这个代码的问题。谢谢!

回答

4

你不能把你的用户名和密码放在$主机本身。您需要先用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 

它应该工作了!