2011-07-22 46 views
0

所以我有一個問題,試圖自動登錄到一個內部網站。我可以發送帖子請求,但在響應中,我總是收到標題連接:關閉。我試圖通過發佈請求,但它似乎仍然迴應連接:關閉。我希望能夠瀏覽網站,所以我需要連接:保持活力,以便我可以發送更多請求。誰能告訴我我做錯了什麼?這裏是代碼:連接不斷關閉?

#usr/bin/perl 
#NetTelnet.pl 

use strict; use warnings; 

#Sign into cfxint Unix something... 
use Net::Telnet; 

# Create a new instance of Net::Telnet, 
my $telnetCon = new Net::Telnet (Timeout => 10, 
          Prompt => '/bash\$ $/') or die "Could not make connection."; 

my $hostname = 'cfxint'; 

# Connect to the host of the users choice         
$telnetCon->open(Host => $hostname, 
      Port => 23) or die "Could not connect to $hostname."; 

use WWW::Mechanize; 

my $mech = WWW::Mechanize->new(cookie_jar => {}); 
&login_alfresco; 


sub login_cxfint { 
#get username and password from user 
my $CXusername = ''; 
my $CXpassword = ''; 

# Recreate the login 
# Wait for the login: message and then enter the username 
$telnetCon->waitfor(match => '/login:/i'); 

# this method adds a \n to the end of the username, it mimics hitting the enter key after entering your username 
$telnetCon->print($CXusername); 

# does the same as the previous command but for the password 
$telnetCon->print($CXpassword); 

#Wait for the login successful message 
$telnetCon->waitfor(); 
} 

sub login_alfresco{ 

my $ALusername = ''; 
my $ALpassword = ''; 
$mech->get('http://documents.ifds.group:8080/alfresco/faces/jsp/login.jsp'); 

my $res = $mech->res; 
my $idfaces = ''; 

if($res->is_success){ 
    my $ff = $res->content; 
    if($ff =~ /id="javax.faces.ViewState" value="(.*?)"/){ 
     $idfaces = $1; 
    } 
    else { 
     print "javax.faces /Regex error?\n"; 
     die; 
    } 
} 

print $idfaces, "\n"; 

#Send the get request for Alfresco 
$mech->post('http://documents.ifds.group:8080/alfresco/faces/jsp/login.jsp',[ 
'loginForm:rediretURL' =>, 
'loginForm:user-name' => $ALusername, 
'loginForm:user-password' => $ALpassword, 
'loginForm:submit' => 'Login', 
'loginForm_SUBMIT' => '1', 
'loginForm:_idcl' => , 
'loginForm:_link_hidden_' => , 
'javax.faces.ViewState' => $idfaces], **'Connection' =>'keep-alive'**); 

$res = $mech->res; 

open ALF, ">Alfresco.html"; 
print ALF $mech->response->as_string; 

if($res->is_success){ 
    my $ff = $res->content; 
    if($ff =~ /id="javax.faces.ViewState" value="(.*?)"/){ 
     $idfaces = $1; 
    } 
    else { 
     print "javax.faces /Regex error?\n"; 
     die; 
    } 
} 
print $idfaces, "\n"; 

#Logout 
$mech->post('http://documents.ifds.group:8080/alfresco/faces/jsp/extension/browse/browse.jsp', [ 
'browse:serach:_option' => '0', 
'browse:search' => , 
'browse:spaces-pages' => '20', 
'browse:content-pages' => '50', 
'browse_SUBMIT' => '1', 
'id' => , 
'browse:modelist' => '', 
'ref'=>'', 
'browse:spacesList:sort' => , 
'browse:_idJsp7' => , 
'browse:sidebar-body:navigator' => , 
'browse:contentRichList:sort' => , 
'browse:act' => 'browse:logout', 
'outcome' => 'logout', 
'browse:panel' => , 
'javax.faces.ViewState' => $idfaces,]) 
} 

回答

1

您可以啓用保活通過使用connection cache

use LWP::ConnCache; 
... 
$mech->conn_cache(LWP::ConnCache->new); 
0

所有的頭意味着連接將在請求完成時關閉,而不是保持打開可能的進一步請求。這是完全正常的,不應該干擾發送請求。

編輯:如果您發送一個連接:保持活動,並且服務器仍在響應Connection:Close,則需要更改服務器配置。 HTTP/1.1的默認值是持久連接,因此服務器必須明確配置爲發送Connection:Close。請參閱Section 8 of RFC2616

+0

但我想繼續發送更多的請求通過網頁瀏覽 – Shahab