连接一直关闭?

时间:2011-07-22 19:52:30

标签: perl http-post

所以我在尝试自动登录内部网站时遇到问题。我能够发送一个帖子请求,但在响应中我总是得到标题连接:关闭。我试图通过post请求传递,但它似乎仍然响应Connection:close。我希望能够浏览网站,所以我需要连接:keep-alive,以便我可以发送更多请求。谁能告诉我我做错了什么?这是代码:

#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,])
}

2 个答案:

答案 0 :(得分:1)

您可以使用connection cache

启用保持活动状态
use LWP::ConnCache;
...
$mech->conn_cache(LWP::ConnCache->new);

答案 1 :(得分:0)

所有标头意味着连接将在请求完成时关闭,而不是保持打开以进一步请求。这是完全正常的,不应干扰发送请求。

编辑:如果您发送的是Connection:Keep-Alive且服务器仍在响应Connection:Close,则需要更改服务器配置。 HTTP / 1.1的默认值是持久连接,因此必须明确配置服务器以发送Connection:Close。请参阅Section 8 of RFC2616