Search code examples
perllwp-useragent

LWP::UserAgent unable to establish 'keep-alive' connection


I am using the LWP::UserAgent to connect to fetch some connection

 use LWP::UserAgent;
 use LWP::ConnCache;
 use  LWP::Debug qw(+);

 my $ua = LWP::UserAgent->new( conn_cache => 1);
 my $cache = $ua->conn_cache(LWP::ConnCache->new( ));
 $ua->conn_cache->total_capacity(undef);
 $ua->cookie_jar({});
 $ua->agent('Mozilla/5.0');
 $ua->add_handler("request_send",  sub { shift->dump; return });
 push @{$ua->requests_redirectable}, 'GET';

 $page = $ua->get('https://www.foo.com');

I tested the script its unable and checked the Requested Header. It does not have any the below key- value pairs.

  Keep-Alive    115
  Connection    keep-alive

valuable input required.


Solution

  • I believe you just need to specify LWP::UserAgent->new(keep_alive => $maxrequests) to enable keepalive. It will automatically set up the connection cache for you.

    I don't see a way to make the number unlimited, though.