3

I want to set the timeout of a request. This is the code, I've got so far.

final HttpClient httpclient = HttpClients.createDefault();
final HttpPost httppost = new HttpPost(address);
httppost.setHeader("Accept", "text/xml");
httppost.setHeader("Content-type", "application/xml; charset=UTF-8");
httppost.setEntity(new StringEntity(body));
final HttpResponse response = httpclient.execute(httppost);
final HttpEntity entity = response.getEntity();

I've tried (does not work, keeps loading and ignores timeout)

// set the connection timeout value to 30 seconds (30000 milliseconds)
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
client = new DefaultHttpClient(httpParams);

and (this one throws java.lang.UnsupportedOperationException)

httpclient = HttpClients.createDefault();
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 6000);

Is there any other way for setting the timout? I don't really need the response so something like an asynchronous request could do the work too.

8
  • isnt this is wrong final HttpClient httpclient; = HttpClients.createDefault(); you have extra ; Commented Apr 1, 2014 at 11:58
  • When you say "does not work", what behaviour are you seeing? Commented Apr 1, 2014 at 11:59
  • @JqueryLearner no it's just a copy and paste error. Commented Apr 1, 2014 at 12:02
  • @christopher nothing it keeps loading and loading and blocking the Java thread. In my program I set it to 3 seconds and it still loads after 10 seconds Commented Apr 1, 2014 at 12:03
  • Have you tried CoreConnectionPNames.TIMEOUT? Commented Apr 1, 2014 at 12:08

1 Answer 1

4

Apache's HttpClient has two separate timeouts: a timeout for how long to wait to establish a TCP connection, and a separate timeout for how long to wait for a subsequent byte of data.

HttpConnectionParams.setConnectionTimeout() is used for establishing a TCP connection, whereas HttpConnectionParams.setSoTimeout() is used while waiting for a subsequent byte of data.

// Creating default HttpClient
HttpClient httpClient = new DefaultHttpClient();
final HttpParams httpParams = httpClient.getParams();

// Setting timeouts
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 30000);

// Rest of your code
final HttpPost httppost = new HttpPost(address);
httppost.setHeader("Accept", "text/xml");
httppost.setHeader("Content-type", "application/xml; charset=UTF-8");
httppost.setEntity(new StringEntity(body));
final HttpResponse response = httpclient.execute(httppost);
final HttpEntity entity = response.getEntity();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.