- java.lang.Object
-
- jdk.incubator.http.HttpClient
-
public abstract class HttpClient extends Object
A container for configuration information common to multipleHttpRequests. All requests are sent through aHttpClient.
Incubating Feature. Will be removed in a future release.HttpClients are immutable and created from a builder returned fromnewBuilder(). Request builders are created by callingHttpRequest.newBuilder().See
HttpRequestfor examples of usage of this API.- Since:
- 9
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classHttpClient.BuilderA builder of immutableHttpClients.static classHttpClient.RedirectDefines automatic redirection policy.static classHttpClient.VersionThe HTTP protocol version.
-
Constructor Summary
Constructors Modifier Constructor Description protectedHttpClient()Creates an HttpClient.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract Optional<Authenticator>authenticator()Returns anOptionalcontaining theAuthenticatorset on this client.abstract Optional<CookieHandler>cookieHandler()Returns anOptionalcontaining this client's CookieHandler.abstract Optional<Executor>executor()Returns anOptionalcontaining this client's Executor.abstract HttpClient.RedirectfollowRedirects()Returns the follow redirects policy for this client.static HttpClient.BuildernewBuilder()Creates a newHttpClientbuilder.static HttpClientnewHttpClient()Returns a new HttpClient with default settings.WebSocket.BuildernewWebSocketBuilder()Creates a newWebSocketbuilder (optional operation).abstract Optional<ProxySelector>proxy()Returns anOptionalcontaining theProxySelectorsupplied to this client.abstract <T> HttpResponse<T>send(HttpRequest req, HttpResponse.BodyHandler<T> responseBodyHandler)Sends the given request using this client, blocking if necessary to get the response.abstract <T> CompletableFuture<HttpResponse<T>>sendAsync(HttpRequest req, HttpResponse.BodyHandler<T> responseBodyHandler)Sends the given request asynchronously using this client and the given response handler.abstract <U,T> CompletableFuture<U>sendAsync(HttpRequest req, HttpResponse.MultiSubscriber<U,T> multiSubscriber)Sends the given request asynchronously using this client and the given multi response handler.abstract SSLContextsslContext()Returns this client'sSSLContext.abstract SSLParameterssslParameters()Returns a copy of this client'sSSLParameters.abstract HttpClient.Versionversion()Returns the HTTP protocol version requested for this client.
-
-
-
Method Detail
-
newHttpClient
public static HttpClient newHttpClient()
Returns a new HttpClient with default settings.Equivalent to
newBuilder().build().The default settings include: the "GET" request method, a preference of HTTP/2, a redirection policy of NEVER, the default proxy selector, and the default SSL context.
- Implementation Note:
- The system-wide default values are retrieved at the time the
HttpClientinstance is constructed. Changing the system-wide values after anHttpClientinstance has been built, for instance, by callingProxySelector.setDefault(ProxySelector)orSSLContext.setDefault(SSLContext), has no effect on already built instances. - Returns:
- a new HttpClient
-
newBuilder
public static HttpClient.Builder newBuilder()
Creates a newHttpClientbuilder.- Returns:
- a
HttpClient.Builder
-
cookieHandler
public abstract Optional<CookieHandler> cookieHandler()
Returns anOptionalcontaining this client's CookieHandler. If noCookieHandlerwas set in this client's builder, then theOptionalis empty.- Returns:
- an
Optionalcontaining this client'sCookieHandler
-
followRedirects
public abstract HttpClient.Redirect followRedirects()
Returns the follow redirects policy for this client. The default value for client's built by builders that do not specify a redirect policy isNEVER.- Returns:
- this client's follow redirects setting
-
proxy
public abstract Optional<ProxySelector> proxy()
Returns anOptionalcontaining theProxySelectorsupplied to this client. If no proxy selector was set in this client's builder, then theOptionalis empty.Even though this method may return an empty optional, the
HttpClientmay still have an non-exposed default proxy selector that is used for sending HTTP requests.- Returns:
- an
Optionalcontaining the proxy selector supplied to this client.
-
sslContext
public abstract SSLContext sslContext()
Returns this client'sSSLContext.If no
SSLContextwas set in this client's builder, then the default context is returned.- Returns:
- this client's SSLContext
-
sslParameters
public abstract SSLParameters sslParameters()
Returns a copy of this client'sSSLParameters.If no
SSLParameterswere set in the client's builder, then an implementation specific default set of parameters, that the client will use, is returned.- Returns:
- this client's
SSLParameters
-
authenticator
public abstract Optional<Authenticator> authenticator()
Returns anOptionalcontaining theAuthenticatorset on this client. If noAuthenticatorwas set in the client's builder, then theOptionalis empty.- Returns:
- an
Optionalcontaining this client'sAuthenticator
-
version
public abstract HttpClient.Version version()
Returns the HTTP protocol version requested for this client. The default value isHttpClient.Version.HTTP_2- Returns:
- the HTTP protocol version requested
-
executor
public abstract Optional<Executor> executor()
Returns anOptionalcontaining this client's Executor. If noExecutorwas set in the client's builder, then theOptionalis empty.Even though this method may return an empty optional, the
HttpClientmay still have an non-exposed default executor that is used for executing asynchronous and dependent tasks.- Returns:
- an
Optionalcontaining this client'sExecutor
-
send
public abstract <T> HttpResponse<T> send(HttpRequest req, HttpResponse.BodyHandler<T> responseBodyHandler) throws IOException, InterruptedException
Sends the given request using this client, blocking if necessary to get the response. The returnedHttpResponse<T>contains the response status, headers, and body ( as handled by given response body handler ).- Type Parameters:
T- the response body type- Parameters:
req- the requestresponseBodyHandler- the response body handler- Returns:
- the response body
- Throws:
IOException- if an I/O error occurs when sending or receivingInterruptedException- if the operation is interruptedIllegalArgumentException- if the request method is not supportedSecurityException- If a security manager has been installed and it deniesaccessto the URL in the given request, or proxy if one is configured. See HttpRequest for further information about security checks.
-
sendAsync
public abstract <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest req, HttpResponse.BodyHandler<T> responseBodyHandler)
Sends the given request asynchronously using this client and the given response handler.The returned completable future completes exceptionally with:
IOException- if an I/O error occurs when sending or receivingIllegalArgumentException- if the request method is not supportedSecurityException- If a security manager has been installed and it deniesaccessto the URL in the given request, or proxy if one is configured. See HttpRequest for further information about security checks.
- Type Parameters:
T- the response body type- Parameters:
req- the requestresponseBodyHandler- the response body handler- Returns:
- a
CompletableFuture<HttpResponse<T>>
-
sendAsync
public abstract <U,T> CompletableFuture<U> sendAsync(HttpRequest req, HttpResponse.MultiSubscriber<U,T> multiSubscriber)
Sends the given request asynchronously using this client and the given multi response handler.The returned completable future completes exceptionally with:
IOException- if an I/O error occurs when sending or receivingIllegalArgumentException- if the request method is not supportedSecurityException- If a security manager has been installed and it deniesaccessto the URL in the given request, or proxy if one is configured. See HttpRequest for further information about security checks.
- Type Parameters:
U- a type representing the aggregated resultsT- a type representing all of the response bodies- Parameters:
req- the requestmultiSubscriber- the multiSubscriber for the request- Returns:
- a
CompletableFuture<U>
-
newWebSocketBuilder
public WebSocket.Builder newWebSocketBuilder()
Creates a newWebSocketbuilder (optional operation).Example
HttpClient client = HttpClient.newHttpClient(); CompletableFuture<WebSocket> ws = client.newWebSocketBuilder() .buildAsync(URI.create("ws://websocket.example.com"), listener);Finer control over the WebSocket Opening Handshake can be achieved by using a custom
HttpClient.Example
InetSocketAddress addr = new InetSocketAddress("proxy.example.com", 80); HttpClient client = HttpClient.newBuilder() .proxy(ProxySelector.of(addr)) .build(); CompletableFuture<WebSocket> ws = client.newWebSocketBuilder() .buildAsync(URI.create("ws://websocket.example.com"), listener);A
WebSocket.Builderreturned from this method is not safe for use by multiple threads without external synchronization.- Implementation Requirements:
- The default implementation of this method throws
UnsupportedOperationException. Clients obtained throughnewHttpClient()ornewBuilder()return aWebSocketbuilder. - Implementation Note:
- Both builder and
WebSockets created with it operate in a non-blocking fashion. That is, their methods do not block before returning aCompletableFuture. Asynchronous tasks are executed in thisHttpClient's executor.When a
CompletionStagereturned fromListener.onClosecompletes, theWebSocketwill send a Close message that has the same code the received message has and an empty reason. - Returns:
- a
WebSocket.Builder - Throws:
UnsupportedOperationException- if thisHttpClientdoes not provide WebSocket support
-
-