Class HttpClient.Builder

  • Enclosing class:
    HttpClient

    public abstract static class HttpClient.Builder
    extends Object
    A builder of immutable HttpClients.
    Incubating Feature. Will be removed in a future release.

    Builders are created by invoking newBuilder. Each of the setter methods modifies the state of the builder and returns the same instance. Builders are not thread-safe and should not be used concurrently from multiple threads without external synchronization.

    Since:
    9
    • Constructor Detail

      • Builder

        protected Builder()
        Creates a Builder.
    • Method Detail

      • cookieHandler

        public abstract HttpClient.Builder cookieHandler​(CookieHandler cookieHandler)
        Sets a cookie handler.
        Parameters:
        cookieHandler - the cookie handler
        Returns:
        this builder
      • sslContext

        public abstract HttpClient.Builder sslContext​(SSLContext sslContext)
        Sets an SSLContext.

        If this method is not invoked prior to building, then newly built clients will use the default context, which is normally adequate for client applications that do not need to specify protocols, or require client authentication.

        Parameters:
        sslContext - the SSLContext
        Returns:
        this builder
      • sslParameters

        public abstract HttpClient.Builder sslParameters​(SSLParameters sslParameters)
        Sets an SSLParameters.

        If this method is not invoked prior to building, then newly built clients will use a default, implementation specific, set of parameters.

        Some parameters which are used internally by the HTTP Client implementation (such as the application protocol list) should not be set by callers, as they may be ignored. The contents of the given object are copied.

        Parameters:
        sslParameters - the SSLParameters
        Returns:
        this builder
      • executor

        public abstract HttpClient.Builder executor​(Executor executor)
        Sets the executor to be used for asynchronous and dependent tasks.

        If this method is not invoked prior to building, a default executor is created for each newly built HttpClient. The default executor uses a cached thread pool, with a custom thread factory.

        Implementation Note:
        If a security manager has been installed, the thread factory creates threads that run with an access control context that has no permissions.
        Parameters:
        executor - the Executor
        Returns:
        this builder
      • followRedirects

        public abstract HttpClient.Builder followRedirects​(HttpClient.Redirect policy)
        Specifies whether requests will automatically follow redirects issued by the server.

        If this method is not invoked prior to building, then newly built clients will use a default redirection policy of NEVER.

        Parameters:
        policy - the redirection policy
        Returns:
        this builder
      • version

        public abstract HttpClient.Builder version​(HttpClient.Version version)
        Requests a specific HTTP protocol version where possible.

        If this method is not invoked prior to building, then newly built clients will prefer HTTP/2.

        If set to HTTP/2, then each request will attempt to upgrade to HTTP/2. If the upgrade succeeds, then the response to this request will use HTTP/2 and all subsequent requests and responses to the same origin server will use HTTP/2. If the upgrade fails, then the response will be handled using HTTP/1.1

        Parameters:
        version - the requested HTTP protocol version
        Returns:
        this builder
      • priority

        public abstract HttpClient.Builder priority​(int priority)
        Sets the default priority for any HTTP/2 requests sent from this client. The value provided must be between 1 and 256 (inclusive).
        Parameters:
        priority - the priority weighting
        Returns:
        this builder
        Throws:
        IllegalArgumentException - if the given priority is out of range
      • authenticator

        public abstract HttpClient.Builder authenticator​(Authenticator a)
        Sets an authenticator to use for HTTP authentication.
        Parameters:
        a - the Authenticator
        Returns:
        this builder
      • build

        public abstract HttpClient build()
        Returns a new HttpClient built from the current state of this builder.
        Returns:
        this builder