2012-10-01 55 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 

它應該工作了!