开发者

Connecting keeps closing?

so i'm having a problem trying to automatically login to a internal website. I'm able to send a post request but in the response I always get the Header Connection: close. I've tried to pass is through the post reque开发者_StackOverflow中文版st but it still seems to respond with Connection: close. I want to be able to navigate through the website so I need the Connection: keep-alive so that i can send more request. Could anyone tell me what I'm doing wrong? here's the code:

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


You can enable keep-alive by using a connection cache:

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


All that header means is that the connection will be closed upon completion of the request, instead of being kept open for possible further requests. This is perfectly normal and should not interfere with sending the request.

EDIT: If you're sending a Connection:Keep-Alive and the server is still responding with Connection:Close, then the server configuration needs to be changed. The default for HTTP/1.1 is persistent connections, so the server must explicitly be configured to send Connection:Close. See Section 8 of RFC2616.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜