Module Java - Part2
Module Java - Part2
Nested Classes
Modifier and Type
Class
Description
static interface
HttpClient.Builder
A builder of HTTP Clients.
static enum
HttpClient.Redirect
Defines the automatic redirection policy.
static enum
HttpClient.Version
The HTTP protocol version.
Constructor Summary
Constructors
Modifier
Constructor
Description
protected
HttpClient()
Creates an HttpClient.
Method Summary
All MethodsStatic MethodsInstance MethodsAbstract MethodsConcrete Methods
Modifier and Type
Method
Description
abstract Optional<Authenticator>
authenticator()
Returns an Optional containing the Authenticator set on this client.
abstract Optional<Duration>
connectTimeout()
Returns an Optional containing the connect timeout duration for this client.
abstract Optional<CookieHandler>
cookieHandler()
Returns an Optional containing this client's CookieHandler.
abstract Optional<Executor>
executor()
Returns an Optional containing this client's Executor.
abstract HttpClient.Redirect
followRedirects()
Returns the follow redirects policy 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()
Creates a new WebSocket builder (optional operation).
abstract Optional<ProxySelector>
proxy()
Returns an Optional containing the ProxySelector supplied to this client.
abstract <T> HttpResponse<T>
send(HttpRequest request, 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 request, HttpResponse.BodyHandler<T>
responseBodyHandler)
Sends the given request asynchronously using this client with the given response body
handler.
abstract <T> CompletableFuture<HttpResponse<T>>
sendAsync(HttpRequest request, HttpResponse.BodyHandler<T>
responseBodyHandler, HttpResponse.PushPromiseHandler<T>
pushPromiseHandler)
Sends the given request asynchronously using this client with the given response body
handler and push promise handler.
abstract SSLContext
sslContext()
Returns this client's SSLContext.
abstract SSLParameters
sslParameters()
Returns a copy of this client's SSLParameters.
abstract HttpClient.Version
version()
Returns the preferred HTTP protocol version for this client.
Methods declared in class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll,
toString, wait, wait, wait
Constructor Details
HttpClient
protected HttpClient()
Creates an HttpClient.
Method Details
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 an HttpClient
instance has been built, for instance, by calling
ProxySelector.setDefault(ProxySelector) or
SSLContext.setDefault(SSLContext), has no effect on already built
instances.
Returns:
a new HttpClient
Throws:
UncheckedIOException - if necessary underlying IO resources required to
build a new HttpClient cannot be allocated.
newBuilder
public static HttpClient.Builder newBuilder()
Creates a new HttpClient builder.
Builders returned by this method create instances of the default HttpClient
implementation.
Returns:
an HttpClient.Builder
cookieHandler
public abstract Optional<CookieHandler> cookieHandler()
Returns an Optional containing this client's CookieHandler. If no
CookieHandler was set in this client's builder, then the Optional is empty.
Returns:
an Optional containing this client's CookieHandler
connectTimeout
public abstract Optional<Duration> connectTimeout()
Returns an Optional containing the connect timeout duration for this client. If
the connect timeout duration was not set in the client's builder, then the
Optional is empty.
Returns:
an Optional containing this client's connect timeout duration
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 is NEVER.
Returns:
this client's follow redirects setting
proxy
public abstract Optional<ProxySelector> proxy()
Returns an Optional containing the ProxySelector supplied to this client. If
no proxy selector was set in this client's builder, then the Optional is empty.
Even though this method may return an empty optional, the HttpClient may
still have a 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's SSLContext.
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's SSLParameters.
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 an Optional containing the Authenticator set on this client. If no
Authenticator was set in the client's builder, then the Optional is empty.
Returns:
an Optional containing this client's Authenticator
version
public abstract HttpClient.Version version()
Returns the preferred HTTP protocol version for this client. The default value is
HttpClient.Version.HTTP_2
Implementation Note:
Constraints may also affect the selection of protocol version. For example, if
HTTP/2 is requested through a proxy, and if the implementation does not
support this mode, then HTTP/1.1 may be used
Returns:
the HTTP protocol version requested
executor
public abstract Optional<Executor> executor()
Returns an Optional containing this client's Executor. If no Executor was
set in the client's builder, then the Optional 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's Executor
send
public abstract <T> HttpResponse<T> send(HttpRequest request,
HttpResponse.BodyHandler<T> responseBodyHandler)
throws IOException,
InterruptedException
Sends the given request using this client, blocking if necessary to get the
response. The returned HttpResponse<T> contains the response status,
headers, and body ( as handled by given response body handler ).
If the operation is interrupted, the default HttpClient implementation
attempts to cancel the HTTP exchange and InterruptedException is
thrown. No guarantee is made as to exactly when the cancellation request may
be taken into account. In particular, the request might still get sent to the
server, as its processing might already have started asynchronously in another
thread, and the underlying resources may only be released asynchronously.
● With HTTP/1.1, an attempt to cancel may cause the underlying
Type Parameters:
T - the response body type
Parameters:
request - the request
responseBodyHandler - the response body handler
Returns:
the response
Throws:
IOException - if an I/O error occurs when sending or receiving
InterruptedException - if the operation is interrupted
IllegalArgumentException - if the request argument is not a request that
could have been validly built as specified by HttpRequest.Builder.
SecurityException - If a security manager has been installed and it denies
access to the URL in the given request, or proxy if one is configured. See
security checks for further information.
sendAsync
public abstract <T>
HttpResponse.BodyHandler<T> responseBodyHandler)
Sends the given request asynchronously using this client with the given response
body handler.
Equivalent to: sendAsync(request, responseBodyHandler, null).
Type Parameters:
T - the response body type
Parameters:
request - the request
responseBodyHandler - the response body handler
Returns:
a CompletableFuture<HttpResponse<T>>
Throws:
IllegalArgumentException - if the request argument is not a request that
could have been validly built as specified by HttpRequest.Builder.
sendAsync
public abstract <T>
HttpResponse.BodyHandler<T> responseBodyHandler,
HttpResponse.PushPromiseHandler<T> pushPromiseHandler)
Sends the given request asynchronously using this client with the given
response body handler and push promise handler.
The returned completable future, if completed successfully, completes with an
HttpResponse<T> that contains the response status, headers, and body ( as
handled by given response body handler ).
Push promises received, if any, are handled by the given
pushPromiseHandler. A null valued pushPromiseHandler rejects any
push promises.
The returned completable future completes exceptionally with:
● IOException - if an I/O error occurs when sending or receiving
Type Parameters:
T - the response body type
Parameters:
request - the request
responseBodyHandler - the response body handler
pushPromiseHandler - push promise handler, may be null
Returns:
a CompletableFuture<HttpResponse<T>>
Throws:
IllegalArgumentException - if the request argument is not a request that
could have been validly built as specified by HttpRequest.Builder.
newWebSocketBuilder
public WebSocket.Builder newWebSocketBuilder()
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);
.build();
CompletableFuture<WebSocket> ws = client.newWebSocketBuilder()
.buildAsync(URI.create("ws://websocket.example.com"),
listener);
Implementation Requirements:
The default implementation of this method throws
UnsupportedOperationException. Clients obtained through
newHttpClient() or newBuilder() return a WebSocket builder.
Implementation Note:
Both builder and WebSockets created with it operate in a non-blocking fashion.
That is, their methods do not block before returning a CompletableFuture.
Asynchronous tasks are executed in this HttpClient's executor.
When a CompletionStage returned from Listener.onClose completes, the
WebSocket 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 this HttpClient does not provide
WebSocket support