Connection Reset error in HttpClient

BKSingh picture BKSingh · Mar 27, 2014 · Viewed 8.7k times · Source

This is scenario I am testing. The steps are as follows

  1. Create an instance of HttpClient(org.apache.commons.httpclient.HttpClient)3.1
  2. Use the instance to get response from a url (http://www.example.com/submitData)
  3. Bring down the server hosting the url mentioned in step 2.
  4. Bring up the url
  5. Wait till the url is up
  6. Use the instance of HttpClient created in step 1 to get response from the url (http://www.example.com/getData)

I get an error - Connection Reset. Can anyone help me understand.

I have used the following params in the HttpClient

    httpClient.getParams().setParameter("http.socket.timeout", new Integer(0));
    httpClient.getParams().setParameter("http.connection.stalecheck", new Boolean(true));

Edited after comments from Peter This is how I create the HttpClient

HttpClient httpClient = new HttpClient();
AuthScope authScope = new AuthScope("www.example.com", 80, AuthScope.ANY_REALM);
Credentials defaultcreds = new UsernamePasswordCredentials("username", "pwd");
httpClient.getState().setCredentials(authScope, defaultcreds);
httpClient.getParams().setParameter("http.socket.timeout", new Integer(0));
httpClient.getParams().setParameter("http.connection.stalecheck", new Boolean(true));

To establish connection, I use the following

GetMethod getMethod = new GetMethod("http://"+httpClient.getHostConfiguration().getHost()+"/getData");
int statusCode = httpClient.executeMethod(getMethod);

I want the httpclient instance to preserve the hostname and port and credentials.

This is exact execption message that I get - "Connection reset".

More info - I am executing the test from eclipse. If I run the test case in debug mode, I dont get this exception.

Answer

PeterMmm picture PeterMmm · Mar 27, 2014

I think the behaviour is reasonable because when the server goes down the client socket on the server goes away too. The client still holds old socket but this socket is gone on the server. You should reconnect from client.

AFAIK the stale parameter allows the client to close the connection the clean way (without Exception) but not prevent from disconnection.