Java HTTP client request with specified timeout

I would like to make BIT (built-in tests) on several servers in my cloud. I need a failed request with a large timeout.

How do I do this using java?

Trying to do something like below does not 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("http://192.168.20.43"); HttpResponse response = client.execute(request); System.out.println(response.toString()); return null; } public static void main(String[] args) throws ClientProtocolException, IOException { nodeBIT(""); } } 

- EDIT: clarify which library is used -

I am using httpclient from apache, here is the corresponding pom.xml section

  <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.0.1</version> <type>jar</type> </dependency> 
+70
java timeout network-protocols
Jun 08 2018-10-18T00-06-08
source share
9 answers
 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); 
+106
Jun 09 '10 at 1:24
source

If you are using Http Client version 4.3 and higher, you should use this:

 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30 * 1000).build(); HttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build(); 
+87
Oct 03 '13 at 7:29
source

HttpParams is deprecated in the new Apache HTTPClient library. Using the code provided by Laz leads to failure warnings.

I suggest using RequestConfig instead of your HttpGet or HttpPost instance:

 final RequestConfig params = RequestConfig.custom().setConnectTimeout(3000).setSocketTimeout(3000).build(); httpPost.setConfig(params); 
+23
Jun 16 '14 at 12:53 on
source

It looks like you're using the HttpClient API, which I don't know anything about, but you can write something similar to this using the Java kernel.

 try { HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); con.setRequestMethod("HEAD"); con.setConnectTimeout(5000); //set timeout to 5 seconds return (con.getResponseCode() == HttpURLConnection.HTTP_OK); } catch (java.net.SocketTimeoutException e) { return false; } catch (java.io.IOException e) { return false; } 
+10
Jun 08 '10 at 19:32
source

I found that setting the timeout settings in HttpConnectionParams and HttpConnectionManager did not solve our case. We are limited to using org.apache.commons.httpclient version 3.0.1.

I ended up using java.util.concurrent.ExecutorService to track the call to HttpClient.executeMethod() .

Here's a small, standalone example.

 import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.EntityEnclosingMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.concurrent.*; /** * @author Jeff Kirby * @since <pre>Jun 17, 2011</pre> */ public class Example { private static final String SITE = "http://some.website.com/upload"; private static final int TIME_OUT_SECS = 5; // upload a file and return the response as a string public String post(File file) throws IOException, InterruptedException { final Part[] multiPart = { new FilePart("file", file.getName(), file) }; final EntityEnclosingMethod post = new PostMethod(SITE); post.setRequestEntity(new MultipartRequestEntity(multiPart, post.getParams())); final ExecutorService executor = Executors.newSingleThreadExecutor(); final List<Future<Integer>> futures = executor.invokeAll(Arrays.asList(new KillableHttpClient(post)), TIME_OUT_SECS, TimeUnit.SECONDS); executor.shutdown(); if(futures.get(0).isCancelled()) { throw new IOException(SITE + " has timed out. It has taken more than " + TIME_OUT_SECS + " seconds to respond"); } return post.getResponseBodyAsString(); } private static class KillableHttpClient implements Callable<Integer> { private final EntityEnclosingMethod post; private KillableHttpClient(EntityEnclosingMethod post) { this.post = post; } public Integer call() throws Exception { return new HttpClient().executeMethod(post); } } } 
+7
Jun 17 '11 at 19:08
source

The specified method with the highest Laz value is deprecated from version 4.3 onwards. Therefore, it would be better if the user requested a configuration object and then created an HTTP client

  private CloseableHttpClient createHttpClient() { CloseableHttpClient httpClient; CommonHelperFunctions helperFunctions = new CommonHelperFunctions(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(306); cm.setDefaultMaxPerRoute(108); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(15000) .setSocketTimeout(15000).build(); httpClient = HttpClients.custom() .setConnectionManager(cm) .setDefaultRequestConfig(requestConfig).build(); return httpClient; } 

PoolingHttpClientConnectionManager is a user who sets the maximum number of connections by default and the maximum number of connections per route. I installed it as 306 and 108 respectively. The default values ​​will not be sufficient for most cases.

To set the timeout: I used the RequestConfig object. You can also set the Time Time Request Timeout property to set the timeout for connecting to the connection manager.

+6
Nov 03 '14 at 8:26
source

This has already been mentioned in the benvoliot comment above. But, I think it’s worth the top-level position, because I’m sure I scratched my head. I am posting this in case this helps someone else.

I wrote a simple test client, and the CoreConnectionPNames.CONNECTION_TIMEOUT timeout CoreConnectionPNames.CONNECTION_TIMEOUT fine in this case. The request is canceled if the server does not respond.

Inside the server code, I really tried to check, however, identical code never expires.

Changing the timeout for socket connection activity ( CoreConnectionPNames.SO_TIMEOUT ) rather than the HTTP connection ( CoreConnectionPNames.CONNECTION_TIMEOUT ) CoreConnectionPNames.CONNECTION_TIMEOUT problem for me.

Also read the Apache docs carefully: http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/params/CoreConnectionPNames.html#CONNECTION_TIMEOUT

Pay attention to the bit that says

Please note that this parameter can only be applied to connections bound to a specific local address.

I hope this saves someone else the whole head scratch I went through. This will teach me not to read the documentation completely.

+5
Apr 23 '13 at 17:41
source

HttpConnectionParams.setSoTimeout(params, 10*60*1000);// for 10 mins i have set the timeout

You can also determine your required timeout.

+1
Oct. 25 '12 at 5:59
source

Op later claimed to use Apache Commons HttpClient 3.0.1

  HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); client.getHttpConnectionManager().getParams().setSoTimeout(5000); 
+1
Jul 10 '14 at 14:34
source



All Articles