0

How to set time out period for HttpDefaultClient?

For the below code I want to set some time out so that it will not hang for more time.

String methodName = "getResponseFromGlobalService";
logger.info(methodName + " started " + "with" + restUrl + "inputs" + jsonObject.toString());
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(restUrl);
StringBuffer outputDocument = new StringBuffer();
StringEntity input1 = new StringEntity(jsonObject.toString());
input1.setContentType("application/json");
postRequest.setEntity(input1);
HttpResponse response = httpClient.execute(postRequest);

if (response.getStatusLine().getStatusCode() != 200) {
  throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
}

BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

String output;
logger.info("Output from Server .... \n");
while ((output = br.readLine()) != null) {
  outputDocument.append(output);
}

JSONObject json = (JSONObject) JSONSerializer.toJSON(outputDocument.toString());
logger.info(methodName + " ended ");
return json;
ivan.sim
  • 8,972
  • 8
  • 47
  • 63
Rasmita jena
  • 183
  • 2
  • 3
  • 7
  • Duplicate of http://stackoverflow.com/questions/3000214/java-http-client-request-with-defined-timeout – aksappy Apr 24 '15 at 05:55
  • Please consider upvoting or accepting the answer if it helped you solved the problem. Otherwise, please provide info. on what isn't working. Thanks! – ivan.sim Apr 28 '15 at 18:04

1 Answers1

0

If you are using DefaultHttpClient 3.x, then the two methods you will need are HttpConnectionParams.html.setConnectionTimeout() and HttpConnectionParams.html.setSoTimeout(), which set the connection timeout and the socket timeout, respectively. The connection timeout is the duration to wait for a connection to be established, while the socket timeout is the duration to wait for the data.

So you will use them like this:

HttpConnectionParams httpParams = new HttpConnectionParams();

int connectionTimeout = 30000; // some timeout in milliseconds
httpParams.setConnectionTimeout(connectionTimeout );

int socketTimeout = 30000; // some timeout in milliseconds
httpParams.setSoTimeout(socketTimeout);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);

Now if you are using DefaultHttpClient 4.x, (in which case you should be using HttpClientBuilder instead of DefaultHttpClient), then these are the methods that will be relevant:

  1. RequestConfig.Builder.setConnectTimeout
  2. RequestConfig.Builder.setSocketTimeout
  3. RequestConfig.Builder.setConnectionTimeout

Here's an example.

int connectTimeout = 50000;
int requestTimeout = 50000;
int socketimeout = 50000;
RequestConfig requestConfig = 
    RequestConfig.custom()
        .setConnectTimeout(connectTimeout)
        .setConnectionRequestTimeout(requestTimeout)
        .setSocketTimeout(socketTimeout)
        .build();
HttpClient httpClient = 
    HttpClientBuilder.create()
        .setDefaultRequestConfig(requestConfig )
        .build();
ivan.sim
  • 8,972
  • 8
  • 47
  • 63