Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LWP::ConnCache - 500 Server closed connection

Status
Not open for further replies.

Kirsle

Programmer
Jan 21, 2006
1,179
US
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:
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. :)
 
Did you got any response or did you find out any solution to your problem.

I am also getting the same

"500 Server closed connection without sending any data back"

Apparently it seems to be the same reason as you mentioned above that the server has closed connection with your agent.

Waiting for your reply.
you can also contact me if possible for you at qasimraza@msn.com

Regards,

Qasim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top