Maxim Veksler Maxim Veksler - 8 months ago 62
HTTP Question

Java HTTP Client Request with defined timeout

I would like to make BIT (Built in tests) to a number of server in my cloud. I need the request to fail on large timeout.

How should I do this with java?

Trying something like the below does not seem to work.

public class TestNodeAliveness {
public static NodeStatus nodeBIT(String elasticIP) throws ClientProtocolException, IOException {
HttpClient client = new DefaultHttpClient();
client.getParams().setIntParameter("http.connection.timeout", 1);

HttpUriRequest request = new HttpGet("");
HttpResponse response = client.execute(request);

return null;

public static void main(String[] args) throws ClientProtocolException, IOException {

-- EDIT: Clarify what library is being used --

I'm using httpclient from apache, here is the relevant pom.xml section


laz laz
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;


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