Class HttpClient

java.lang.Object
com.luciad.net.http.HttpClient
All Implemented Interfaces:
IHttpClient, AutoCloseable

public final class HttpClient extends Object implements AutoCloseable, IHttpClient
An HTTP Client, implementation of the 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
  • Method Details

    • finalize

      protected void finalize()
      Overrides:
      finalize in class Object
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • newBuilder

      @NotNull public static HttpClient.Builder newBuilder()
      Creates the new HttpClient builder.
      Returns:
      the new HttpClient builder.
    • send

      @NotNull public HttpResponse send(@NotNull HttpRequest request) throws IOException
      Returns the 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.

      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 the 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

      invalid reference
      Canceled
      .
      Specified by:
      send in interface IHttpClient
      Parameters:
      request - HTTP request.
      token - Cancellation token.
      Returns:
      the HTTP response.
      Throws:
      IOException - when loading the data fails.
    • setHttpRequestOptions

      public void setHttpRequestOptions(@NotNull HttpRequestOptions requestOptions)
      Sets the HTTP request options sent with every request.
      Parameters:
      requestOptions - the request options.
    • getHttpRequestOptions

      @Nullable public HttpRequestOptions 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.