Interface HttpClient
- All Superinterfaces:
AutoCloseable
To create a new HTTP client, use a builder. To send requests and retrieve their responses, use send(java.net.http.HttpRequest, java.net.http.HttpResponse.BodyHandler<T>).
To release resources held by the client, use close().
HTTP clients are thread-safe. Each client maintains its own connection pool and SSLContext. For efficiency reasons, clients should be reused whenever possible.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic HttpClient.Builderbuilder()Creates a newHTTPClientbuilder with default settings.voidclose()Closes this client.static HttpClientReturns a client that throwsAssertionErroron every attempt to send a request.<T> HttpResponse<T> send(HttpRequest request, HttpResponse.BodyHandler<T> responseBodyHandler) Sends an HTTP request.
-
Method Details
-
builder
Creates a newHTTPClientbuilder with default settings.The default settings are:
- Connect timeout: 60 seconds
- Request timeout: 60 seconds
- CA certificates: none (falls back to the JVM's default SSL context)
-
dummyClient
Returns a client that throwsAssertionErroron every attempt to send a request. -
send
<T> HttpResponse<T> send(HttpRequest request, HttpResponse.BodyHandler<T> responseBodyHandler) throws IOException Sends an HTTP request. The response body is processed by the given body handler.If the request does not specify a timeout, the client's request timeout is used. If the request does not specify a preferred HTTP version, HTTP/2 is used. The request's
User-Agentheader is set to the client'sUser-Agentheader.Depending on the given body handler, this method blocks until response headers or the entire response body has been received. If response headers are not received within the request timeout,
HttpTimeoutExceptionis thrown.For additional information on how to use this method, see
HttpClient.send(java.net.http.HttpRequest, java.net.http.HttpResponse.BodyHandler<T>).- Throws:
IOException- if an I/O error occurs when sending or receivingHttpClientInitException- if an error occurs while initializing a lazy client
-
close
void close()Closes this client.This method makes a best effort to release the resources held by this client in a timely manner. This may involve waiting for pending requests to complete.
Subsequent calls to this method have no effect. Subsequent calls to any other method throw
IllegalStateException.- Specified by:
closein interfaceAutoCloseable
-