3

I am using below code to post JSON data using LWP::useragent. I want to keep my session open and post two requests but it seems that its not working on linux machine (two POST requests are sent in two sessions instead of one).

Any suggestions? thanks in advance

#!/usr/bin/perl

use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;

open (JSON, "json3.txt") or die "$!";
$raw_string1 = do{ local $/ = undef; <JSON>; 
};



my $req = HTTP::Request->new(POST => 'http://www.example.com');


$hdr1 = 'User-Agent';
$val1 = 'Java/1.7.0_45';

$hdr2 = 'Connection';
$val2 = 'keep-alive';

$hdr3 = 'Accept';
$val3 = 'application/json, application/*+json';

$hdr4 = 'Host';
$val4 = 'example.com';

$hdr5 = 'Content-Type';
$val5 = 'application/json;charset=UTF-8';


$req -> header($hdr3 => $val3);
$req -> header($hdr5 => $val5);
$req -> header($hdr1 => $val1);
$req -> header($hdr4 => $val4);
$req -> header($hdr2 => $val2);



$req->content_type("application/json");

$req->content("$raw_string1");

my $ua = LWP::UserAgent->new(keep_alive => 1);
$res = $ua->request($req);
print $res->content;
$res = $ua->request($req);
print $res->content; 
Arunesh Singh
  • 3,489
  • 18
  • 26
user2896215
  • 507
  • 7
  • 20
  • When you use keep_alive option then just LWP::ConnCache is set up which Get/sets the number of connection that will be cached. Now what error/response are you getting when run this script? – sandeep Jul 23 '15 at 05:55

2 Answers2

3

Keep-Alive is just recommending the server to not close the TCP connection after the request because their will be more requests. The server does not need to follow the recommendation and in fact lots of servers don't to keep the number of open TCP connections low which all use up resources on the system.

Apart from that you should not need to set the Connection and Host header explicitly.

I've tried the following simplified example and a packet capture shows that keep alive is working if the server supports it (LWP 6.05). Supporting means that the server keeps the connection open and does not set a "Connection: close" header and either uses HTTP/1.1 or uses HTTP/1.0 together with "Connection: keep-alive" header.

my $req = HTTP::Request->new(POST => 'http://www.example.com/');
$req->content_type("application/json");
$req->content("foo");

my $ua = LWP::UserAgent->new(keep_alive => 1);
$res = $ua->request($req);
print $res->content;
$res = $ua->request($req);
print $res->content;
Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • I checked the packet captures. its not the backend server who is closing connection but the client is. – user2896215 Jul 23 '15 at 14:23
  • I've tried a simplified example and it works. Are you sure that the server does not reply with "Connection: close" or issues a HTTP/1.0 response (which implicitly defined Connection: close) ? – Steffen Ullrich Jul 23 '15 at 14:35
  • its resolved...it wasn't due to backend server closing the connection. i think i was using old perl (5.10) with old fedora version. I spun a new instance of CentOs and its working on it. thanks! – user2896215 Jul 24 '15 at 03:31
1

its resolved...it wasn't due to backend server closing the connection. i think i was using old perl (5.10) with old fedora version. I spun a new instance of CentOs and its working on it. thanks

user2896215
  • 507
  • 7
  • 20