Class HttpClient
- All Implemented Interfaces:
IHttpClient,AutoCloseable
IHttpClient interface.
An HttpClient can be used to send requests and retrieve their responses. An HttpClient is created through a builder. The builder can be used to configure client states, like: a proxy selector, an authenticator provider, etc.
Example
HttpRequest request = HttpRequest.newBuilder().uri("https://foo.com/").method(HttpRequestMethod.POST).body(new DataEntity(buffer, "application/json")).build();
HttpClient client = HttpClient.newBuilder().proxySelector(proxySelector).credentialProvider(credentialsProvider).build();
HttpResponse response = client.send(request);
System.out.println(response.getStatusCode());
This class is thread-safe, i.e. it can handle multiple concurrent requests.
- Since:
- 2023.0
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidClears the HTTP request options sent with every request, if any.voidclose()protected voidfinalize()Returns the HTTP request options sent with every request.static HttpClient.BuilderCreates the newHttpClientbuilder.send(HttpRequest request) Returns theHTTP responsefor the givenHTTP request.send(HttpRequest request, CancellationToken token) Returns theHTTP responsefor the givenHTTP request.voidsetHttpRequestOptions(HttpRequestOptions requestOptions) Sets the HTTP request options sent with every request.
-
Method Details
-
finalize
protected void finalize() -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
newBuilder
Creates the newHttpClientbuilder.- Returns:
- the new
HttpClientbuilder.
-
send
Returns theHTTP responsefor the givenHTTP request.This method is synchronous and blocks until the request has been sent and the response has been received.
- Parameters:
request- HTTP request.- Returns:
- the HTTP response.
- Throws:
IOException- when loading the data fails.
-
send
@NotNull public HttpResponse send(@NotNull HttpRequest request, @NotNull CancellationToken token) throws IOException Returns theHTTP responsefor the givenHTTP request.This method is synchronous and blocks until the request has been sent and the response has been received.
The request process can be canceled by the means of the
cancellation token. It is up to the implementer to verify that the cancellation token state is set totrueduring the operation. When set totruethis indicates that an HTTP response is no longer needed.- Specified by:
sendin interfaceIHttpClient- Parameters:
request- HTTP request.token- Cancellation token.- Returns:
- the HTTP response.
- Throws:
IOException- when loading the data fails.
-
setHttpRequestOptions
Sets the HTTP request options sent with every request.- Parameters:
requestOptions- the request options.
-
getHttpRequestOptions
Returns the HTTP request options sent with every request.- Returns:
- the HTTP request options sent with every request.
-
clearHttpRequestOptions
public void clearHttpRequestOptions()Clears the HTTP request options sent with every request, if any.
-