Module bus.http

Class HttpBuilder

java.lang.Object
org.miaixz.bus.http.plugin.httpz.HttpBuilder

public class HttpBuilder extends Object
A fluent builder for configuring and creating an Httpz.Client instance. This class wraps an Httpd.Builder to provide a simplified, chainable interface for setting up the underlying HTTP client.
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • HttpBuilder

      public HttpBuilder()
      Creates a new builder with a default client configuration.
    • HttpBuilder

      public HttpBuilder(Httpd httpd)
      Creates a new builder based on the configuration of an existing Httpd client.
      Parameters:
      httpd - The client whose configuration will be copied.
  • Method Details

    • connectTimeout

      public HttpBuilder connectTimeout(long timeout, TimeUnit unit)
      Sets the default connect timeout for new connections.
      Parameters:
      timeout - The timeout value.
      unit - The time unit for the timeout.
      Returns:
      This builder instance for chaining.
    • readTimeout

      public HttpBuilder readTimeout(long timeout, TimeUnit unit)
      Sets the default read timeout for new connections.
      Parameters:
      timeout - The timeout value.
      unit - The time unit for the timeout.
      Returns:
      This builder instance for chaining.
    • writeTimeout

      public HttpBuilder writeTimeout(long timeout, TimeUnit unit)
      Sets the default write timeout for new connections.
      Parameters:
      timeout - The timeout value.
      unit - The time unit for the timeout.
      Returns:
      This builder instance for chaining.
    • pingInterval

      public HttpBuilder pingInterval(long interval, TimeUnit unit)
      Sets the ping interval for web socket connections.
      Parameters:
      interval - The interval value.
      unit - The time unit for the interval.
      Returns:
      This builder instance for chaining.
    • proxy

      public HttpBuilder proxy(Proxy proxy)
      Sets the HTTP proxy that will be used by connections created by this client.
      Parameters:
      proxy - The proxy to use.
      Returns:
      This builder instance for chaining.
    • proxySelector

      public HttpBuilder proxySelector(ProxySelector proxySelector)
      Sets the proxy selection policy for this client.
      Parameters:
      proxySelector - The proxy selector.
      Returns:
      This builder instance for chaining.
    • cookieJar

      public HttpBuilder cookieJar(CookieJar cookieJar)
      Sets the cookie handler for this client.
      Parameters:
      cookieJar - The cookie jar.
      Returns:
      This builder instance for chaining.
    • cache

      public HttpBuilder cache(Cache cache)
      Sets the response cache to be used by this client.
      Parameters:
      cache - The cache.
      Returns:
      This builder instance for chaining.
    • dns

      public HttpBuilder dns(DnsX dnsX)
      Sets the DNS service used to lookup IP addresses for hostnames.
      Parameters:
      dnsX - The custom DNS service.
      Returns:
      This builder instance for chaining.
    • socketFactory

      public HttpBuilder socketFactory(SocketFactory socketFactory)
      Sets the socket factory used to create connections.
      Parameters:
      socketFactory - The socket factory.
      Returns:
      This builder instance for chaining.
    • sslSocketFactory

      @Deprecated public HttpBuilder sslSocketFactory(SSLSocketFactory sslSocketFactory)
      Sets the SSL socket factory used to secure HTTPS connections.
      Parameters:
      sslSocketFactory - The SSL socket factory.
      Returns:
      This builder instance for chaining.
    • sslSocketFactory

      public HttpBuilder sslSocketFactory(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager)
      Sets the SSL socket factory and trust manager used to secure HTTPS connections.
      Parameters:
      sslSocketFactory - The SSL socket factory.
      trustManager - The trust manager.
      Returns:
      This builder instance for chaining.
    • hostnameVerifier

      public HttpBuilder hostnameVerifier(HostnameVerifier hostnameVerifier)
      Sets the verifier used to confirm that response certificates apply to the requested hostnames.
      Parameters:
      hostnameVerifier - The hostname verifier.
      Returns:
      This builder instance for chaining.
    • certificatePinner

      public HttpBuilder certificatePinner(CertificatePinner certificatePinner)
      Sets the certificate pinner used to constrain which certificates are trusted.
      Parameters:
      certificatePinner - The certificate pinner.
      Returns:
      This builder instance for chaining.
    • authenticator

      public HttpBuilder authenticator(Authenticator authenticator)
      Sets the authenticator used to respond to challenges from origin servers.
      Parameters:
      authenticator - The authenticator.
      Returns:
      This builder instance for chaining.
    • proxyAuthenticator

      public HttpBuilder proxyAuthenticator(Authenticator proxyAuthenticator)
      Sets the authenticator used to respond to challenges from proxy servers.
      Parameters:
      proxyAuthenticator - The proxy authenticator.
      Returns:
      This builder instance for chaining.
    • connectionPool

      public HttpBuilder connectionPool(ConnectionPool connectPool)
      Sets the connection pool used to recycle HTTP and HTTPS connections.
      Parameters:
      connectPool - The connection pool.
      Returns:
      This builder instance for chaining.
    • followSslRedirects

      public HttpBuilder followSslRedirects(boolean followProtocolRedirects)
      Configures this client to follow redirects from HTTPS to HTTP and from HTTP to HTTPS.
      Parameters:
      followProtocolRedirects - true to follow redirects across protocols.
      Returns:
      This builder instance for chaining.
    • followRedirects

      public HttpBuilder followRedirects(boolean followRedirects)
      Configures this client to follow redirects.
      Parameters:
      followRedirects - true to follow redirects.
      Returns:
      This builder instance for chaining.
    • retryOnConnectionFailure

      public HttpBuilder retryOnConnectionFailure(boolean retryOnConnectionFailure)
      Configures this client to retry or not retry IO failures.
      Parameters:
      retryOnConnectionFailure - true to retry on connection failures.
      Returns:
      This builder instance for chaining.
    • dispatcher

      public HttpBuilder dispatcher(Dispatcher dispatcher)
      Sets the dispatcher used to execute asynchronous requests.
      Parameters:
      dispatcher - The dispatcher.
      Returns:
      This builder instance for chaining.
    • protocols

      public HttpBuilder protocols(List<org.miaixz.bus.core.net.Protocol> protocols)
      Configures the protocols used by this client.
      Parameters:
      protocols - The list of protocols.
      Returns:
      This builder instance for chaining.
    • connectionSpecs

      public HttpBuilder connectionSpecs(List<ConnectionSuite> connectSuites)
      Configures the connection specs used by this client.
      Parameters:
      connectSuites - The list of connection specs.
      Returns:
      This builder instance for chaining.
    • addInterceptor

      public HttpBuilder addInterceptor(Interceptor interceptor)
      Adds an application interceptor to this client.
      Parameters:
      interceptor - The interceptor to add.
      Returns:
      This builder instance for chaining.
    • addNetworkInterceptor

      public HttpBuilder addNetworkInterceptor(Interceptor interceptor)
      Adds a network interceptor to this client.
      Parameters:
      interceptor - The interceptor to add.
      Returns:
      This builder instance for chaining.
    • getBuilder

      public Httpd.Builder getBuilder()
      Returns the underlying Httpd.Builder for advanced configuration.
      Returns:
      The raw builder.
    • sslContext

      public HttpBuilder sslContext(SSLContext sslContext)
      A convenience method to configure a custom SSLContext. This method also sets a permissive trust manager and hostname verifier.
      Parameters:
      sslContext - The SSL context to use.
      Returns:
      This builder instance for chaining.
    • build

      public Httpz.Client build()
      Creates an Httpz.Client with the configured settings.
      Returns:
      A new Httpz.Client instance.