- java.lang.Object
-
- jdk.incubator.http.HttpClient
-
public abstract class HttpClient extends Object
A container for configuration information common to multipleHttpRequest
s. All requests are sent through aHttpClient
.
Incubating Feature. Will be removed in a future release.HttpClient
s are immutable and created from a builder returned fromnewBuilder()
. Request builders are created by callingHttpRequest.newBuilder()
.See
HttpRequest
for examples of usage of this API.- Since:
- 9
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
HttpClient.Builder
A builder of immutableHttpClient
s.static class
HttpClient.Redirect
Defines automatic redirection policy.static class
HttpClient.Version
The HTTP protocol version.
-
Constructor Summary
Constructors Modifier Constructor Description protected
HttpClient()
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 anOptional
containing theAuthenticator
set on this client.abstract Optional<CookieHandler>
cookieHandler()
Returns anOptional
containing this client's CookieHandler.abstract Optional<Executor>
executor()
Returns anOptional
containing this client's Executor.abstract HttpClient.Redirect
followRedirects()
Returns the follow redirects policy for this client.static HttpClient.Builder
newBuilder()
Creates a newHttpClient
builder.static HttpClient
newHttpClient()
Returns a new HttpClient with default settings.WebSocket.Builder
newWebSocketBuilder()
Creates a newWebSocket
builder (optional operation).abstract Optional<ProxySelector>
proxy()
Returns anOptional
containing theProxySelector
supplied 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 SSLContext
sslContext()
Returns this client'sSSLContext
.abstract SSLParameters
sslParameters()
Returns a copy of this client'sSSLParameters
.abstract HttpClient.Version
version()
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
HttpClient
instance is constructed. Changing the system-wide values after anHttpClient
instance 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 newHttpClient
builder.- Returns:
- a
HttpClient.Builder
-
cookieHandler
public abstract Optional<CookieHandler> cookieHandler()
Returns anOptional
containing this client's CookieHandler. If noCookieHandler
was set in this client's builder, then theOptional
is empty.- Returns:
- an
Optional
containing 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 anOptional
containing theProxySelector
supplied to this client. If no proxy selector was set in this client's builder, then theOptional
is empty.Even though this method may return an empty optional, the
HttpClient
may still have an non-exposed default proxy selector that is used for sending HTTP requests.- Returns:
- an
Optional
containing the proxy selector supplied to this client.
-
sslContext
public abstract SSLContext sslContext()
Returns this client'sSSLContext
.If no
SSLContext
was 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
SSLParameters
were 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 anOptional
containing theAuthenticator
set on this client. If noAuthenticator
was set in the client's builder, then theOptional
is empty.- Returns:
- an
Optional
containing 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 anOptional
containing this client's Executor. If noExecutor
was set in the client's builder, then theOptional
is empty.Even though this method may return an empty optional, the
HttpClient
may still have an non-exposed default executor that is used for executing asynchronous and dependent tasks.- Returns:
- an
Optional
containing 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 deniesaccess
to 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 deniesaccess
to 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 deniesaccess
to 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 newWebSocket
builder (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.Builder
returned 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 aWebSocket
builder. - Implementation Note:
- Both builder and
WebSocket
s 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
CompletionStage
returned fromListener.onClose
completes, theWebSocket
will send a Close message that has the same code the received message has and an empty reason. - Returns:
- a
WebSocket.Builder
- Throws:
UnsupportedOperationException
- if thisHttpClient
does not provide WebSocket support
-
-