public final class HttpClient extends Object implements AutoCloseable, IHttpClient
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.
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.
Modifier and Type | Class and Description |
---|---|
static class |
HttpClient.Builder
Builder for the HttpClient class. |
Modifier and Type | Method and Description |
---|---|
void |
clearHttpRequestOptions()
Clears the HTTP request options sent with every request, if any.
|
void |
close() |
protected void |
finalize() |
HttpRequestOptions |
getHttpRequestOptions()
Returns the HTTP request options sent with every request.
|
static HttpClient.Builder |
newBuilder()
Creates the new
HttpClient builder. |
HttpResponse |
send(HttpRequest request)
Returns the
HTTP response for the given HTTP request . |
HttpResponse |
send(HttpRequest request,
CancellationToken token)
Returns the
HTTP response for the given HTTP request . |
void |
setHttpRequestOptions(HttpRequestOptions requestOptions)
Sets the HTTP request options sent with every request.
|
public void close()
close
in interface AutoCloseable
@NotNull public static HttpClient.Builder newBuilder()
HttpClient
builder.HttpClient
builder.@NotNull public HttpResponse send(@NotNull HttpRequest request) throws IOException
HTTP response
for the given HTTP request
.
This method is synchronous and blocks until the request has been sent and the response has been received.
request
- HTTP request.IOException
- when loading the data fails.@NotNull public HttpResponse send(@NotNull HttpRequest request, @NotNull CancellationToken token) throws IOException
HTTP response
for the given HTTP 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 to true
during the operation. When set to true
this indicates that a HTTP response is no longer needed. The return value then can be an ErrorInfo
object with the error code Canceled
.
send
in interface IHttpClient
request
- HTTP request.token
- Cancellation token.IOException
- when loading the data fails.public void setHttpRequestOptions(@NotNull HttpRequestOptions requestOptions)
requestOptions
- the request options.@Nullable public HttpRequestOptions getHttpRequestOptions()
public void clearHttpRequestOptions()