Class HttpClient.Builder

  • Enclosing class:
    HttpClient


    public abstract static class HttpClient.Builder
    extends Object
    A builder of immutable HttpClients. HttpClient.Builders are created by calling HttpClient.newBuilder().
    Incubating Feature. Will be removed in a future release.

    Each of the setter methods in this class modifies the state of the builder and returns this (ie. the same instance). The methods are not synchronized and should not be called from multiple threads without external synchronization.

    build() returns a new HttpClient each time it is called.

    Since:
    9
    • Constructor Detail

      • Builder

        protected Builder​()
    • Method Detail

      • cookieManager

        public abstract HttpClient.Builder cookieManager​(CookieManager cookieManager)
        Sets a cookie manager.
        Parameters:
        cookieManager - the cookie manager
        Returns:
        this builder
      • sslContext

        public abstract HttpClient.Builder sslContext​(SSLContext sslContext)
        Sets an SSLContext. If a security manager is set, then the caller must have the NetPermission ("setSSLContext")

        The effect of not calling this method, is that a default SSLContext is used, 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
        Throws:
        SecurityException - if a security manager is set and the caller does not have any required permission
      • sslParameters

        public abstract HttpClient.Builder sslParameters​(SSLParameters sslParameters)
        Sets an SSLParameters. If this method is not called, then a default set of parameters are used. The contents of the given object are copied. Some parameters which are used internally by the HTTP protocol implementation (such as application protocol list) should not be set by callers, as they are ignored.
        Parameters:
        sslParameters - the SSLParameters
        Returns:
        this builder
      • executor

        public abstract HttpClient.Builder executor​(Executor executor)
        Sets the executor to be used for asynchronous tasks. If this method is not called, a default executor is set, which is the one returned from Executors.newCachedThreadPool.
        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. This setting can be overridden on each request. The default value for this setting is 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 not set, the version defaults to HttpClient.Version.HTTP_2. If HttpClient.Version.HTTP_2 is set, 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
      • proxy

        public abstract HttpClient.Builder proxy​(ProxySelector selector)
        Sets a ProxySelector for this client. If no selector is set, then no proxies are used. If a null parameter is given then the system wide default proxy selector is used.
        Implementation Note:
        ProxySelector.of(InetSocketAddress) provides a ProxySelector which uses one proxy for all requests.
        Parameters:
        selector - the ProxySelector
        Returns:
        this builder
      • 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 HttpClient built from the current state of this builder.
        Returns:
        this builder