CERN | Present | HTTPS | Keep-Alive | Connection Pooling | Caching | HTTP2 | WebSocket | SSE | Reactive | License | |
Java HttpURLConnection 11 | cmp | m (1) |
wc
|
wc
| m (7) |
| (2) | GPLv2 Classpath Exception | |||
Java HttpClient 11 | cmp | , Flow | GPLv2 Classpath Exception | ||||||||
OkHttp 3.6.0 | used | partially | Apache v2 | ||||||||
OkHttp 4.9.2 | cmp (3) | Apache v2 | |||||||||
Apache httpcomponents-client 4.5.13 | used | (4) | (4) | Apache v2 | |||||||
Apache httpcomponents-client 5.1 | cmp (6) | (4) | , Reactive Streams | Apache v2 | |||||||
Jetty Client 11.0.6 | nu | Apache v2 | |||||||||
Spring reactive Webclient | nu | partially | ne | ne | no | ne | ne | ne | , Reactor | Apache v2 | |
\– Reactor Netty 0.9.12 | nu | partially | , Reactor | Apache v2 | |||||||
\– Jetty Reactive 3.0.4 | nu | , Reactive Streams | EPL 2.0 |
@Contract(threading=SAFE)
WebClient
is through one of the static factory methods:" WebClient.create()
HttpClient
participates in the global Reactor Netty resources held in reactor.netty.http.HttpResources
, including event loop threads and a connection pool. This is the recommended mode, since fixed, shared resources are preferred for event loop concurrency. In this mode global resources remain active until the process exits."
retrieve()
, when using exchange()
, it is the responsibility of the application to consume any response content regardless of the scenario (success, error, unexpected data, etc). Not doing so can cause a memory leak."
java.util.concurrent.Flow
and Reactive Streams ( http://www.reactive-streams.org/ ), Project Reactor and RxJava are implementations of the Reactive Streams Interface.
// Globally System.setProperty("sun.net.client.defaultReadTimeout", "10000"); // ms System.setProperty("sun.net.client.defaultConnectTimeout", "3000"); // ms URL url = new URL("https://www.gsi.de/start/aktuelles"); // URL url = new URL("http://localhost:4321"); // read timeout: while (true) ; do nc -l -p 4321 ; sleep 0.2 ; done // URL url = new URL("http://10.255.255.1:4321"); // non routable, connection timeout https://stackoverflow.com/a/904609/405793 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // Locally connection.setConnectTimeout(2000); // ms connection.setReadTimeout(2000); // ms try (InputStream is = connection.getInputStream()) { // url.openStream() would be shorter but does not provide content encoding info String body = IOUtils.toString(is, connection.getContentEncoding()); System.out.println(body.substring(0, 512)); }
HttpClient client = HttpClient.newBuilder() // .connectTimeout(Duration.ofSeconds(2)) // .build(); HttpRequest request = HttpRequest.newBuilder() // .uri(URI.create("https://www.gsi.de/start/aktuelles")) // .timeout(Duration.ofSeconds(2)) // .GET() // .build(); HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); System.out.println(response.body().substring(0, 512));
OkHttpClient client = new OkHttpClient.Builder() // .connectTimeout(2, TimeUnit.SECONDS) // .readTimeout(2, TimeUnit.SECONDS) // .writeTimeout(2, TimeUnit.SECONDS) // // .callTimeout(4, TimeUnit.SECONDS) // Not in 3.6.0 .build(); Request request = new Request.Builder() // .url("https://www.gsi.de/start/aktuelles") // .build(); try (Response response = client.newCall(request).execute()) { System.out.println(response.body().string().substring(0, 512)); }
connect
, read
, write
is 10 seconds
call
(time from start to finish) is 0 (unlimited)
// String url = "https://www.gsi.de/start/aktuelles"; // String url = "http://localhost:4321"; // read timeout: while (true) ; do nc -l -p 4321 ; sleep 0.2 ; done String url = "http://10.255.255.1:4321"; // non routable, connection timeout https://stackoverflow.com/a/904609/405793 RequestConfig requestConfig = RequestConfig.custom() // .setSocketTimeout(2000) // .setConnectTimeout(2000) // .setConnectionRequestTimeout(2000) // .build(); CloseableHttpClient httpClient = HttpClients.custom() // .setDefaultRequestConfig(requestConfig) // .build(); HttpGet httpGet = new HttpGet(url); // httpGet.setConfig(requestConfig); // alternative to setting it centrally or to overwrite it CloseableHttpResponse response = httpClient.execute(httpGet); try { System.out.println(EntityUtils.toString(response.getEntity()).substring(0, 512)); } finally { response.close(); httpClient.close(); // on app/service shutdown }
getConnectionRequestTimeout
- "Returns the timeout in milliseconds used when requesting a connection from the connection manager." (Potentially limited per target host on the client side)
sun.net.client
Properties are not used!
String url = "https://www.gsi.de/start/aktuelles"; // String url = "http://localhost:4321"; // read timeout: while (true) ; do nc -l -p 4321 ; sleep 0.2 ; done // String url = "http://10.255.255.1:4321"; // non routable, connection timeout https://stackoverflow.com/a/904609/405793 RequestConfig requestConfig = RequestConfig.custom() // .setResponseTimeout(Timeout.ofSeconds(2)) // .setConnectTimeout(Timeout.ofSeconds(2)) // .setConnectionRequestTimeout(Timeout.ofSeconds(2)) // .build(); CloseableHttpClient httpClient = HttpClients.custom() // .setDefaultRequestConfig(requestConfig) // .build(); HttpGet httpGet = new HttpGet(url); // httpGet.setConfig(requestConfig); // alternative to setting it centrally or to overwrite it CloseableHttpResponse response = httpClient.execute(httpGet); try { System.out.println(EntityUtils.toString(response.getEntity()).substring(0, 512)); } finally { response.close(); httpClient.close(); // on app/service shutdown }
Connect
timeout (changed since v4.5) "A timeout value of zero is interpreted as an infinite timeout. Default: 3 minutes"
Connection Request
(connection pool) timeout (changed since v4.5) "A timeout value of zero is interpreted as an infinite timeout. Default: 3 minutes"
Response
timeout, formerly socket timeout (changed since v4.5) "Default: null" endpoint.setSocketTimeout(responseTimeout);
in org.apache.hc.client5.http.impl.classic.InternalExecRuntime.execute(String, ClassicHttpRequest, HttpClientContext)
String url = "https://www.gsi.de/start/aktuelles"; // String url = "http://localhost:4321"; // read timeout: while (true) ; do nc -l -p 4321 ; sleep 0.2 ; done // String url = "http://10.255.255.1:4321"; // non routable, connection timeout https://stackoverflow.com/a/904609/405793 String body = Request.get(url) // .responseTimeout(Timeout.ofSeconds(2)) // .connectTimeout(Timeout.ofSeconds(2)) // .execute() // Uses a global connection pool max 100 connections per route, total max of 200 connections, 3s connection request timeout // .execute(customizableHttpClient) .returnContent() // .asString(); System.out.println(body.substring(0, 512));
String url = "https://www.gsi.de/start/aktuelles"; // String url = "http://localhost:4321"; // read timeout: while (true) ; do nc -l -p 4321 ; sleep 0.2 ; done // String url = "http://10.255.255.1:4321"; // non routable, connection timeout https://stackoverflow.com/a/904609/405793 HttpClient httpClient = HttpClient.create() .tcpConfiguration(client -> client.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000) // .doOnConnected(conn -> conn // .addHandlerLast(new ReadTimeoutHandler(2, TimeUnit.SECONDS)) // .addHandlerLast(new WriteTimeoutHandler(2, TimeUnit.SECONDS)))); System.out.println("Calling: " + url); Disposable disposable = httpClient // .get() // .uri(url) // .responseContent() // .aggregate() // .asString() // .subscribe(content -> System.out.println(content.substring(0, 512))); while (!disposable.isDisposed()) { System.out.print("."); Thread.sleep(250); } System.out.println("\ndone");
connection
timeout is 30 seconds
Read
timeout default seems unlimited
, tested 270 seconds
String url = "https://www.gsi.de/start/aktuelles"; // String url = "http://localhost:4321"; // read timeout: while (true) ; do nc -l -p 4321 ; sleep 0.2 ; done // String url = "http://10.255.255.1:4321"; // non routable, connection timeout https://stackoverflow.com/a/904609/405793 HttpClient httpClient = new HttpClient(); httpClient.setConnectTimeout(2000); // ms httpClient.setIdleTimeout(2000); // ms httpClient.setAddressResolutionTimeout(2000); // ms (included in connection timeout) httpClient.start(); Request request = httpClient.newRequest(url); ReactiveRequest reactiveRequest = ReactiveRequest.newBuilder(request).build(); // reactive wrapper Publisher<String> publisher = reactiveRequest.response(ReactiveResponse.Content.asString()); // Subscribe to the Publisher to send the request publisher.subscribe(new Subscriber<String>() { public void onSubscribe(Subscription subscription) { subscription.request(1); // This is where the request is actually sent. } public void onNext(String response) { System.out.println(response.substring(0, 512)); } public void onError(Throwable failure) { System.out.println("\nerror: " + failure); System.exit(1); } public void onComplete() { System.out.println("\ndone"); System.exit(0); } }); System.out.println("Calling: " + url); while (true) { System.out.print("."); Thread.sleep(250); }
connection
timeout is 5 seconds idle
(read) timeout, defaults to 30 second
String url = "https://www.gsi.de/start/aktuelles"; //String url = "http://localhost:4321"; // read timeout: while (true) ; do nc -l -p 4321 ; sleep 0.2 ; done //String url = "http://10.255.255.1:4321"; // non routable, connection timeout https://stackoverflow.com/a/904609/405793 // Just like Httpcomponents4 config RequestConfig config = RequestConfig.custom() // .setConnectTimeout(3000) // ms .setConnectionRequestTimeout(3000) // ms .setSocketTimeout(3000) // ms .build(); CloseableHttpClient client = HttpClientBuilder.create() // .setDefaultRequestConfig(config) // .build(); HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(client); RestTemplate restTemplate = new RestTemplate(httpRequestFactory); restTemplate.setMessageConverters(List.of(new StringHttpMessageConverter())); // Default is json, this allows "String" response type ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, null, String.class); String body = response.getBody(); System.out.println(body.substring(0, 512));
String url = "https://www.gsi.de/start/aktuelles"; // String url = "http://localhost:4321"; // read timeout: while (true) ; do nc -l -p 4321 ; sleep 0.2 ; done // String url = "http://10.255.255.1:4321"; // non routable, connection timeout https://stackoverflow.com/a/904609/405793 SimpleClientHttpRequestFactory httpRequestFactory = new SimpleClientHttpRequestFactory(); httpRequestFactory.setConnectTimeout(3000); // ms httpRequestFactory.setReadTimeout(3000); // ms RestTemplate restTemplate = new RestTemplate(httpRequestFactory); restTemplate.setMessageConverters(List.of(new StringHttpMessageConverter())); // Default is json, this allows "String" response type ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, null, String.class); String body = response.getBody(); System.out.println(body.substring(0, 512));
HttpUrlConnection
, since SimpleClientHttpRequestFactory
is used by default instead of apache!