Hi, I'm developing a module to interface a Website by using LWP::UserAgent. It's using LWP::ConnCache because of all the different HTTP requests it's going to need.
Sometimes a request will fail and the status_line says "500 Server closed connection without sending any data back". I'm assuming that this means the server has closed the connection with my agent, so that the "Keep-Alive" connection is broken.
When the server closes a Keep-Alive connection, does LWP::UserAgent automatically reconnect when I try another request, or do I have to remake the agent object? I'd prefer not to remake it, since it needs cookies too and a new agent would lose its cookies.
Here's some parts of my code:
Thanks.
Sometimes a request will fail and the status_line says "500 Server closed connection without sending any data back". I'm assuming that this means the server has closed the connection with my agent, so that the "Keep-Alive" connection is broken.
When the server closes a Keep-Alive connection, does LWP::UserAgent automatically reconnect when I try another request, or do I have to remake the agent object? I'd prefer not to remake it, since it needs cookies too and a new agent would lose its cookies.
Here's some parts of my code:
Code:
# Creates the agent
sub makeAgent {
my $self = shift;
$self->{browser} = LWP::UserAgent->new;
$self->{browser}->agent ($self->{agent});
$self->{browser}->cookie_jar ( HTTP::Cookies->new (
file => $self->{cookies},
));
$self->{browser}->conn_cache (LWP::ConnCache->new());
}
# Sends HTTP requests (tries to return the content
# of the pages it gets. Automatically requests on
# redirection headers)
sub request {
my ($self,$method,$url,$data) = @_;
$self->debug ("Accessing $method $url...");
if ($method eq 'GET') {
my $reply = $self->{browser}->get ($url);
if ($reply->is_success) {
my ($error,$code) = $self->errorCheck ($reply->content);
if ($error) {
warn "ERROR: $code";
}
return $reply->content;
}
else {
# We may be getting a redirect.
if (defined $reply->headers->{location} && length $reply->headers->{location}) {
$self->debug ("Got a redirection from $url\n"
. "to " . $reply->headers->{location});
return $self->request ('GET',$reply->headers->{location});
}
warn "Could not access $url: " . $reply->status_line . "\n"
. Dumper($reply->headers);
$self->makeAgent if $reply->status_line =~ /^500 Server closed connection/i;
return undef;
}
}
elsif ($method eq 'POST') {
my $reply = $self->{browser}->post ($url, $data);
if ($reply->is_success) {
my ($error,$code) = $self->errorCheck ($reply->content);
if ($error) {
warn "ERROR: $code";
}
return $reply->content;
}
else {
# We may be getting a redirect.
if (defined $reply->headers->{location} && length $reply->headers->{location}) {
$self->debug ("Got a redirection from $url\n"
. "to " . $reply->headers->{location});
return $self->request ('GET',$reply->headers->{location});
}
warn "Could not access $url: " . $reply->status_line . "\n"
. Dumper($reply->headers);
$self->makeAgent if $reply->status_line =~ /^500 Server closed connection/i;
return undef;
}
}
else {
warn "Unsupported HTTP method $method";
return undef;
}
}
Thanks.