HttpClient.Builder (Java SE 10 & JDK 10 )
Keywords: jdk.incubator.http.HttpClient.Builder class

Module jdk.incubator.httpclient
Package jdk.incubator.http

## Class HttpClient.Builder

- java.lang.Object
- - jdk.incubator.http.HttpClient.Builder

- Enclosing class:
    HttpClient

      public abstract static class HttpClient.Builder
      extends Object

  A builder of immutable `HttpClient`s.
  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

- - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `static ProxySelector` | `NO_PROXY` | A proxy selector that always return `Proxy.NO_PROXY` implying a direct connection.

  - ### Constructor Summary

    Constructors
    Modifier | Constructor | Description
    --- | --- | ---
    `protected` | `Builder()` | Creates a Builder.

  - ### Method Summary

    All Methods Instance Methods Abstract Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `abstract HttpClient.Builder` | `authenticator​(Authenticator a)` | Sets an authenticator to use for HTTP authentication.
    `abstract HttpClient` | `build()` | Returns a new `HttpClient` built from the current state of this builder.
    `abstract HttpClient.Builder` | `cookieHandler​(CookieHandler cookieHandler)` | Sets a cookie handler.
    `abstract HttpClient.Builder` | `executor​(Executor executor)` | Sets the executor to be used for asynchronous and dependent tasks.
    `abstract HttpClient.Builder` | `followRedirects​(HttpClient.Redirect policy)` | Specifies whether requests will automatically follow redirects issued by the server.
    `abstract HttpClient.Builder` | `priority​(int priority)` | Sets the default priority for any HTTP/2 requests sent from this client.
    `abstract HttpClient.Builder` | `proxy​(ProxySelector selector)` | Sets a `ProxySelector`.
    `abstract HttpClient.Builder` | `sslContext​(SSLContext sslContext)` | Sets an `SSLContext`.
    `abstract HttpClient.Builder` | `sslParameters​(SSLParameters sslParameters)` | Sets an `SSLParameters`.
    `abstract HttpClient.Builder` | `version​(HttpClient.Version version)` | Requests a specific HTTP protocol version where possible.

    - ### Methods declared in class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### NO_PROXY

          public static final ProxySelector NO_PROXY

      A proxy selector that always return `Proxy.NO_PROXY` implying a direct connection. This is a convenience object that can be passed to `proxy(ProxySelector)` in order to build an instance of `HttpClient` that uses no proxy.

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

    - #### proxy

          public abstract HttpClient.Builder proxy​(ProxySelector selector)

      Sets a `ProxySelector`.
      API Note:
        `ProxySelector.of(InetSocketAddress)` provides a `ProxySelector` which uses a single proxy for all requests. The system-wide proxy selector can be retrieved by `ProxySelector.getDefault()`.
      Implementation Note:
        If this method is not invoked prior to building, then newly built clients will use the default proxy selector, which is normally adequate for client applications. This default behavior can be turned off by supplying an explicit proxy selector to this method, such as `NO_PROXY` or one returned by `ProxySelector.of(InetSocketAddress)`, before calling `build()`.
      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 new `HttpClient` built from the current state of this builder.
      Returns:
        this builder