public abstract class HttpClient extends Object
HttpRequest
s. All requests are sent through a HttpClient
.
HttpClient
s are immutable and created from a builder returned
from newBuilder()
. Request builders are created by calling
HttpRequest.newBuilder()
.
See HttpRequest
for examples of usage of this API.
Modifier and Type | Class | Description |
---|---|---|
static class |
HttpClient.Builder |
A builder of immutable
HttpClient s. |
static class |
HttpClient.Redirect |
Defines automatic redirection policy.
|
static class |
HttpClient.Version |
The HTTP protocol version.
|
Modifier | Constructor | Description |
---|---|---|
protected |
HttpClient() |
Creates an HttpClient.
|
Modifier and Type | Method | Description |
---|---|---|
abstract Optional<Authenticator> |
authenticator() |
Returns an
Optional containing the Authenticator set on
this client. |
abstract Optional<CookieManager> |
cookieManager() |
Returns an
Optional which contains this client's CookieManager . |
abstract Executor |
executor() |
Returns the
Executor set on this client. |
abstract HttpClient.Redirect |
followRedirects() |
Returns the follow-redirects setting for this client.
|
static HttpClient.Builder |
newBuilder() |
Creates a new
HttpClient builder. |
static HttpClient |
newHttpClient() |
Returns a new HttpClient with default settings.
|
WebSocket.Builder |
newWebSocketBuilder(URI uri,
WebSocket.Listener listener) |
Creates a builder of
WebSocket instances connected to the given
URI and receiving events and messages with the given Listener . |
abstract Optional<ProxySelector> |
proxy() |
Returns an
Optional containing the ProxySelector for 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 SSLContext |
sslContext() |
Returns the
SSLContext , if one was set on this client. |
abstract Optional<SSLParameters> |
sslParameters() |
Returns an
Optional containing the SSLParameters set on
this client. |
abstract HttpClient.Version |
version() |
Returns the HTTP protocol version requested for this client.
|
public static HttpClient newHttpClient()
public static HttpClient.Builder newBuilder()
HttpClient
builder.HttpClient.Builder
public abstract Optional<CookieManager> cookieManager()
Optional
which contains this client's CookieManager
. If no CookieManager
was set in this client's builder,
then the Optional
is empty.Optional
containing this client's CookieManager
public abstract HttpClient.Redirect followRedirects()
HttpClient.Redirect.NEVER
public abstract Optional<ProxySelector> proxy()
Optional
containing the ProxySelector
for this client.
If no proxy is set then the Optional
is empty.Optional
containing this client's proxy selectorpublic abstract SSLContext sslContext()
SSLContext
, if one was set on this client. If a security
manager is set, then the caller must have the
NetPermission
("getSSLContext") permission.
If no SSLContext
was set, then the default context is returned.SecurityException
- if the caller does not have permission to get
the SSLContextpublic abstract Optional<SSLParameters> sslParameters()
Optional
containing the SSLParameters
set on
this client. If no SSLParameters
were set in the client's builder,
then the Optional
is empty.Optional
containing this client's SSLParameters
public abstract Optional<Authenticator> authenticator()
Optional
containing the Authenticator
set on
this client. If no Authenticator
was set in the client's builder,
then the Optional
is empty.Optional
containing this client's Authenticator
public abstract HttpClient.Version version()
HttpClient.Version.HTTP_1_1
public abstract Executor executor()
Executor
set on this client. If an
Executor
was not set on the client's builder, then a default
object is returned. The default Executor
is created independently
for each client.public abstract <T> HttpResponse<T> send(HttpRequest req, HttpResponse.BodyHandler<T> responseBodyHandler) throws IOException, InterruptedException
HttpResponse
<T>
contains the
response status, headers, and body ( as handled by given response body
handler ).T
- the response body typereq
- the requestresponseBodyHandler
- the response body handlerIOException
- if an I/O error occurs when sending or receivingInterruptedException
- if the operation is interruptedpublic abstract <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest req, HttpResponse.BodyHandler<T> responseBodyHandler)
T
- the response body typereq
- the requestresponseBodyHandler
- the response body handlerCompletableFuture<HttpResponse<T>>
public abstract <U,T> CompletableFuture<U> sendAsync(HttpRequest req, HttpResponse.MultiProcessor<U,T> multiProcessor)
U
- a type representing the aggregated resultsT
- a type representing all of the response bodiesreq
- the requestmultiProcessor
- the MultiProcessor for the requestCompletableFuture<U>
public WebSocket.Builder newWebSocketBuilder(URI uri, WebSocket.Listener listener)
WebSocket
instances connected to the given
URI and receiving events and messages with the given Listener
.
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);
UnsupportedOperationException
. However, clients obtained through
newHttpClient()
or newBuilder()
provide WebSocket capability.uri
- the WebSocket URIlistener
- the listenerWebSocket
instancesUnsupportedOperationException
- if this HttpClient
does not provide WebSocket support Submit a bug or feature
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 2015, 2017, Oracle and/or its affiliates. 500 Oracle Parkway
Redwood Shores, CA 94065 USA. All rights reserved.
DRAFT 9-Debian+0-9b153-2