Module bus.http

Class Httpd

java.lang.Object
org.miaixz.bus.http.Httpd
All Implemented Interfaces:
Cloneable, NewCall.Factory, WebSocket.Factory

public class Httpd extends Object implements Cloneable, NewCall.Factory, WebSocket.Factory
The core client for making HTTP requests and reading their responses. This class is designed to be efficient by reusing connections with a connection pool. It is recommended to create a single, shared instance of Httpd and reuse it for all HTTP calls.

This class supports both synchronous and asynchronous calls. Asynchronous calls are executed on a background thread pool, which is managed by a Dispatcher.

This class also supports advanced features like HTTP/2 and WebSockets.

Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • Httpd

      public Httpd()
      Constructs a new Httpd instance with default settings.
    • Httpd

      public Httpd(Httpd.Builder builder)
      Constructs a new Httpd instance configured by the given Httpd.Builder.
      Parameters:
      builder - The builder instance containing all configuration parameters.
  • Method Details

    • newCall

      public NewCall newCall(Request request)
      Prepares the request to be executed at some point in the future.
      Specified by:
      newCall in interface NewCall.Factory
      Parameters:
      request - The HTTP request to execute.
      Returns:
      a new NewCall instance that can be used to execute the request.
    • newWebSocket

      public WebSocket newWebSocket(Request request, WebSocketListener listener)
      Uses request to connect a new web socket.
      Specified by:
      newWebSocket in interface WebSocket.Factory
      Parameters:
      request - The WebSocket request object.
      listener - The WebSocket event listener.
      Returns:
      a new WebSocket instance.
    • callTimeoutMillis

      public int callTimeoutMillis()
      Returns the default timeout for complete calls in milliseconds.
      Returns:
      the call timeout in milliseconds.
    • connectTimeoutMillis

      public int connectTimeoutMillis()
      Returns the default timeout for establishing a new connection in milliseconds.
      Returns:
      the connect timeout in milliseconds.
    • readTimeoutMillis

      public int readTimeoutMillis()
      Returns the default timeout for reading data from a connection in milliseconds.
      Returns:
      the read timeout in milliseconds.
    • writeTimeoutMillis

      public int writeTimeoutMillis()
      Returns the default timeout for writing data to a connection in milliseconds.
      Returns:
      the write timeout in milliseconds.
    • pingIntervalMillis

      public int pingIntervalMillis()
      Returns the WebSocket ping interval in milliseconds.
      Returns:
      the ping interval in milliseconds.
    • proxy

      public Proxy proxy()
      Returns the configured HTTP proxy, or null if no proxy is configured.
      Returns:
      the proxy instance, which may be null.
    • proxySelector

      public ProxySelector proxySelector()
      Returns the proxy selector used to choose a proxy for a given URI.
      Returns:
      the proxy selector.
    • cookieJar

      public CookieJar cookieJar()
      Returns the cookie jar used to manage HTTP cookies.
      Returns:
      the CookieJar instance.
    • cache

      public Cache cache()
      Returns the cache used to store responses.
      Returns:
      the Cache instance, which may be null.
    • dns

      public DnsX dns()
      Returns the DNS service used to resolve hostnames.
      Returns:
      the DnsX instance.
    • socketFactory

      public SocketFactory socketFactory()
      Returns the socket factory used for creating plain TCP connections.
      Returns:
      the SocketFactory instance.
    • sslSocketFactory

      public SSLSocketFactory sslSocketFactory()
      Returns the SSL socket factory used for creating HTTPS connections.
      Returns:
      the SSLSocketFactory instance.
    • hostnameVerifier

      public HostnameVerifier hostnameVerifier()
      Returns the hostname verifier used for HTTPS connections.
      Returns:
      the HostnameVerifier instance.
    • certificatePinner

      public CertificatePinner certificatePinner()
      Returns the certificate pinner used to constrain trusted certificates.
      Returns:
      the CertificatePinner instance.
    • authenticator

      public Authenticator authenticator()
      Returns the authenticator for handling challenges from origin servers.
      Returns:
      the Authenticator instance.
    • proxyAuthenticator

      public Authenticator proxyAuthenticator()
      Returns the authenticator for handling challenges from proxy servers.
      Returns:
      the Authenticator instance.
    • connectionPool

      public ConnectionPool connectionPool()
      Returns the connection pool for managing and reusing connections.
      Returns:
      the ConnectionPool instance.
    • followSslRedirects

      public boolean followSslRedirects()
      Returns true if this client follows redirects from HTTPS to HTTP and vice versa.
      Returns:
      true if SSL redirects are followed.
    • followRedirects

      public boolean followRedirects()
      Returns true if this client follows HTTP redirects.
      Returns:
      true if redirects are followed.
    • retryOnConnectionFailure

      public boolean retryOnConnectionFailure()
      Returns true if this client retries requests on connection failures.
      Returns:
      true if retry on connection failure is enabled.
    • dispatcher

      public Dispatcher dispatcher()
      Returns the dispatcher that manages asynchronous requests.
      Returns:
      the Dispatcher instance.
    • protocols

      public List<org.miaixz.bus.core.net.Protocol> protocols()
      Returns the list of protocols supported by this client.
      Returns:
      an immutable list of protocols.
    • connectionSpecs

      public List<ConnectionSuite> connectionSpecs()
      Returns the list of connection specs supported by this client.
      Returns:
      an immutable list of connection specs.
    • interceptors

      public List<Interceptor> interceptors()
      Returns the list of application interceptors.
      Returns:
      an immutable list of interceptors.
    • networkInterceptors

      public List<Interceptor> networkInterceptors()
      Returns the list of network interceptors.
      Returns:
      an immutable list of network interceptors.
    • eventListenerFactory

      public EventListener.Factory eventListenerFactory()
      Returns the factory for creating event listeners.
      Returns:
      the EventListener.Factory instance.
    • newBuilder

      public Httpd.Builder newBuilder()
      Returns a new builder that is a copy of this client's configuration.
      Returns:
      a new Httpd.Builder instance.