- 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<CookieManager>cookieManager()Returns anOptionalwhich contains this client'sCookieManager.abstract Executorexecutor()Returns theExecutorset on this client.abstract HttpClient.RedirectfollowRedirects()Returns the follow-redirects setting for this client.static HttpClient.BuildernewBuilder()Creates a newHttpClientbuilder.static HttpClientnewHttpClient()Returns a new HttpClient with default settings.WebSocket.BuildernewWebSocketBuilder(URI uri, WebSocket.Listener listener)Creates a builder ofWebSocketinstances connected to the given URI and receiving events and messages with the givenListener.abstract Optional<ProxySelector>proxy()Returns anOptionalcontaining theProxySelectorfor 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.MultiProcessor<U,T> multiProcessor)Sends the given request asynchronously using this client and the given multi response handler.abstract SSLContextsslContext()Returns theSSLContext, if one was set on this client.abstract Optional<SSLParameters>sslParameters()Returns anOptionalcontaining theSSLParametersset on this client.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.- Returns:
- a new HttpClient
-
newBuilder
public static HttpClient.Builder newBuilder()
Creates a newHttpClientbuilder.- Returns:
- a
HttpClient.Builder
-
cookieManager
public abstract Optional<CookieManager> cookieManager()
Returns anOptionalwhich contains this client'sCookieManager. If noCookieManagerwas set in this client's builder, then theOptionalis empty.- Returns:
- an
Optionalcontaining this client'sCookieManager
-
followRedirects
public abstract HttpClient.Redirect followRedirects()
Returns the follow-redirects setting for this client. The default value for this setting isHttpClient.Redirect.NEVER- Returns:
- this client's follow redirects setting
-
proxy
public abstract Optional<ProxySelector> proxy()
Returns anOptionalcontaining theProxySelectorfor this client. If no proxy is set then theOptionalis empty.- Returns:
- an
Optionalcontaining this client's proxy selector
-
sslContext
public abstract SSLContext sslContext()
Returns theSSLContext, if one was set on this client. If a security manager is set, then the caller must have theNetPermission("getSSLContext") permission. If noSSLContextwas set, then the default context is returned.- Returns:
- this client's SSLContext
- Throws:
SecurityException- if the caller does not have permission to get the SSLContext
-
sslParameters
public abstract Optional<SSLParameters> sslParameters()
Returns anOptionalcontaining theSSLParametersset on this client. If noSSLParameterswere set in the client's builder, then theOptionalis empty.- Returns:
- an
Optionalcontaining this client'sSSLParameters
-
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 Executor executor()
Returns theExecutorset on this client. If anExecutorwas not set on the client's builder, then a default object is returned. The defaultExecutoris created independently for each client.- Returns:
- this client's Executor
-
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 interrupted
-
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.- 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.MultiProcessor<U,T> multiProcessor)
Sends the given request asynchronously using this client and the given multi response handler.- Type Parameters:
U- a type representing the aggregated resultsT- a type representing all of the response bodies- Parameters:
req- the requestmultiProcessor- the MultiProcessor for the request- Returns:
- a
CompletableFuture<U>
-
newWebSocketBuilder
public WebSocket.Builder newWebSocketBuilder(URI uri, WebSocket.Listener listener)
Creates a builder ofWebSocketinstances connected to the given URI and receiving events and messages with the givenListener.Example
HttpClient client = HttpClient.newHttpClient(); WebSocket.Builder builder = client.newWebSocketBuilder( 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(); WebSocket.Builder builder = client.newWebSocketBuilder( URI.create("ws://websocket.example.com"), listener);- Implementation Requirements:
- The default implementation of this method throws
UnsupportedOperationException. However, clients obtained throughnewHttpClient()ornewBuilder()provide WebSocket capability. - Parameters:
uri- the WebSocket URIlistener- the listener- Returns:
- a builder of
WebSocketinstances - Throws:
UnsupportedOperationException- if thisHttpClientdoes not provide WebSocket support
-
-