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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Clears the HTTP request options sent with every request, if any.void
close()
protected void
finalize()
Returns the HTTP request options sent with every request.static HttpClient.Builder
Creates the newHttpClient
builder.send
(HttpRequest request) Returns theHTTP response
for the givenHTTP request
.send
(HttpRequest request, CancellationToken token) Returns theHTTP response
for the givenHTTP request
.void
setHttpRequestOptions
(HttpRequestOptions requestOptions) Sets the HTTP request options sent with every request.
-
Method Details
-
finalize
protected void finalize() -
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-
newBuilder
Creates the newHttpClient
builder.- Returns:
- the new
HttpClient
builder.
-
send
Returns theHTTP response
for 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 response
for 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 totrue
during the operation. When set totrue
this indicates that a HTTP response is no longer needed. The return value then can be anErrorInfo
object with the error codeinvalid reference
Canceled
- Specified by:
send
in 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.
-