--- old/src/java.net.http/share/classes/java/net/http/HttpClient.java 2018-08-07 15:12:59.000000000 +0100 +++ new/src/java.net.http/share/classes/java/net/http/HttpClient.java 2018-08-07 15:12:58.000000000 +0100 @@ -34,6 +34,7 @@ import java.net.URLPermission; import java.security.AccessController; import java.security.PrivilegedAction; +import java.time.Duration; import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; @@ -84,6 +85,7 @@ *
{@code    HttpClient client = HttpClient.newBuilder()
  *        .version(Version.HTTP_1_1)
  *        .followRedirects(Redirect.NORMAL)
+ *        .connectTimeout(Duration.ofSeconds(20))
  *        .proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80)))
  *        .authenticator(Authenticator.getDefault())
  *        .build();
@@ -94,7 +96,7 @@
  * 

Asynchronous Example *

{@code    HttpRequest request = HttpRequest.newBuilder()
  *        .uri(URI.create("https://foo.com/"))
- *        .timeout(Duration.ofMinutes(1))
+ *        .timeout(Duration.ofMinutes(2))
  *        .header("Content-Type", "application/json")
  *        .POST(BodyPublishers.ofFile(Paths.get("file.json")))
  *        .build();
@@ -197,6 +199,26 @@
         public Builder cookieHandler(CookieHandler cookieHandler);
 
         /**
+         * Sets the connect timeout duration for this client.
+         *
+         * 

In the case where a new connection needs to be established, if + * the connection cannot be established within the given {@code + * duration}, then {@link HttpClient#send(HttpRequest,BodyHandler) + * HttpClient::send} throws an {@link HttpConnectTimeoutException}, or + * {@link HttpClient#sendAsync(HttpRequest,BodyHandler) + * HttpClient::sendAsync} completes exceptionally with an + * {@code HttpConnectTimeoutException}. If a new connection does not + * need to be established, for example if a connection can be reused + * from a previous request, then this timeout duration has no effect. + * + * @param duration the duration to allow the underlying connection to be + * established + * @return this builder + * @throws IllegalArgumentException if the duration is non-positive + */ + public Builder connectTimeout(Duration duration); + + /** * Sets an {@code SSLContext}. * *

If this method is not invoked prior to {@linkplain #build() @@ -345,6 +367,17 @@ public abstract Optional cookieHandler(); /** + * Returns an {@code Optional} containing the connect timeout duration + * for this client. If the {@linkplain Builder#connectTimeout(Duration) + * connect timeout duration} was not set in the client's builder, then the + * {@code Optional} is empty. + * + * @return an {@code Optional} containing this client's connect timeout + * duration + */ + public abstract Optional connectTimeout(); + + /** * Returns the follow redirects policy for this client. The default value * for client's built by builders that do not specify a redirect policy is * {@link HttpClient.Redirect#NEVER NEVER}.