Module bus.http

Class Httpx

java.lang.Object
org.miaixz.bus.http.Httpx

public class Httpx extends Object
A utility class for sending HTTP requests with a simplified API. This class provides static methods for common request types like GET, POST, PUT, DELETE, and HEAD. It uses a shared, lazily-initialized Httpd instance for all requests.
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • Httpx

      public Httpx()
      Default constructor that initializes the client with default timeouts.
    • Httpx

      public Httpx(X509TrustManager x509TrustManager)
      Constructor that initializes the client with a custom trust manager.
      Parameters:
      x509TrustManager - The trust manager for SSL connections.
    • Httpx

      public Httpx(int connTimeout, int readTimeout, int writeTimeout)
      Constructor that initializes the client with custom timeouts.
      Parameters:
      connTimeout - The connection timeout in seconds.
      readTimeout - The read timeout in seconds.
      writeTimeout - The write timeout in seconds.
    • Httpx

      public Httpx(int connTimeout, int readTimeout, int writeTimeout, int maxRequests, int maxRequestsPerHost, int maxIdleConnections, int keepAliveDuration)
      Constructor that initializes the client with detailed custom settings.
      Parameters:
      connTimeout - The connection timeout in seconds.
      readTimeout - The read timeout in seconds.
      writeTimeout - The write timeout in seconds.
      maxRequests - The maximum number of parallel requests.
      maxRequestsPerHost - The maximum number of parallel requests per host.
      maxIdleConnections - The maximum number of idle connections.
      keepAliveDuration - The keep-alive duration for idle connections in minutes.
    • Httpx

      public Httpx(DnsX dns, HttpProxy httpProxy, int connTimeout, int readTimeout, int writeTimeout, int maxRequests, int maxRequestsPerHost, int maxIdleConnections, int keepAliveDuration)
      Constructor that initializes the client with DNS and proxy settings.
      Parameters:
      dns - The custom DNS resolver.
      httpProxy - The HTTP proxy configuration.
      connTimeout - The connection timeout in seconds.
      readTimeout - The read timeout in seconds.
      writeTimeout - The write timeout in seconds.
      maxRequests - The maximum number of parallel requests.
      maxRequestsPerHost - The maximum number of parallel requests per host.
      maxIdleConnections - The maximum number of idle connections.
      keepAliveDuration - The keep-alive duration for idle connections in minutes.
    • Httpx

      public Httpx(DnsX dns, HttpProxy httpProxy, int connTimeout, int readTimeout, int writeTimeout, int maxRequests, int maxRequestsPerHost, int maxIdleConnections, int keepAliveDuration, SSLSocketFactory sslSocketFactory, X509TrustManager x509TrustManager, HostnameVerifier hostnameVerifier)
      The main constructor that initializes the underlying Httpd client with all possible configurations.
      Parameters:
      dns - The custom DNS resolver.
      httpProxy - The HTTP proxy configuration.
      connTimeout - The connection timeout in seconds.
      readTimeout - The read timeout in seconds.
      writeTimeout - The write timeout in seconds.
      maxRequests - The maximum number of parallel requests.
      maxRequestsPerHost - The maximum number of parallel requests per host.
      maxIdleConnections - The maximum number of idle connections.
      keepAliveDuration - The keep-alive duration for idle connections in minutes.
      sslSocketFactory - The SSL socket factory for HTTPS connections.
      x509TrustManager - The trust manager for SSL connections.
      hostnameVerifier - The hostname verifier for HTTPS connections.
  • Method Details

    • get

      public static String get(String url)
      Sends a simple GET request with the default UTF-8 encoding.
      Parameters:
      url - The URL to send the request to.
      Returns:
      The response body as a String.
    • get

      public static String get(String url, String charset)
      Sends a simple GET request with a custom encoding.
      Parameters:
      url - The URL to send the request to.
      charset - The custom character set for the response.
      Returns:
      The response body as a String.
    • get

      public static String get(String url, boolean isAsync)
      Sends a GET request, either synchronously or asynchronously.
      Parameters:
      url - The URL to send the request to.
      isAsync - If true, the request is sent asynchronously.
      Returns:
      The response body as a String (for synchronous requests) or an empty string (for asynchronous requests).
    • get

      public static String get(String url, Map<String,String> formMap)
      Sends a GET request with query parameters and default UTF-8 encoding.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of query parameters.
      Returns:
      The response body as a String.
    • get

      public static void get(String url, Callback callback)
      Sends an asynchronous GET request with a callback.
      Parameters:
      url - The URL to send the request to.
      callback - The callback to handle the response or failure.
    • get

      public static String get(String url, Map<String,String> formMap, Map<String,String> headerMap)
      Sends a GET request with query and header parameters and default UTF-8 encoding.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of query parameters.
      headerMap - A map of header parameters.
      Returns:
      The response body as a String.
    • get

      public static String get(String url, Map<String,String> formMap, Map<String,String> headerMap, String charset)
      Sends a GET request with query parameters, header parameters, and a custom encoding.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of query parameters.
      headerMap - A map of header parameters.
      charset - The custom character set for the response.
      Returns:
      The response body as a String.
    • post

      public static void post(String url, Map<String,String> formMap, Callback callback)
      Sends an asynchronous POST request with form data and a callback.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      callback - The callback to handle the response or failure.
    • post

      public static String post(String url)
      Sends a simple POST request with an empty body.
      Parameters:
      url - The URL to send the request to.
      Returns:
      The response body as a String.
    • post

      public static String post(String url, Map<String,String> formMap)
      Sends a POST request with form data using application/x-www-form-urlencoded content type.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      Returns:
      The response body as a String.
    • post

      public static String post(String url, String data, String contentType)
      Sends a POST request with a raw data body and a specified content type.
      Parameters:
      url - The URL to send the request to.
      data - The raw request body data.
      contentType - The content type of the request body.
      Returns:
      The response body as a String.
    • post

      public static String post(String url, Map<String,String> formMap, String contentType)
      Sends a POST request with form data and a specified content type.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      contentType - The content type of the request body.
      Returns:
      The response body as a String.
    • post

      public static String post(String url, Map<String,String> formMap, Map<String,String> headerMap)
      Sends a POST request with form data and header parameters.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      headerMap - A map of header parameters.
      Returns:
      The response body as a String.
    • post

      public static String post(String url, String data, String contentType, String charset)
      Sends a POST request with a raw data body, a specified content type, and a custom encoding.
      Parameters:
      url - The URL to send the request to.
      data - The raw request body data.
      contentType - The content type of the request body.
      charset - The custom character set for the request and response.
      Returns:
      The response body as a String.
    • post

      public static String post(String url, String data, Map<String,String> headerMap, String contentType)
      Sends a POST request with a raw data body, header parameters, and a specified content type.
      Parameters:
      url - The URL to send the request to.
      data - The raw request body data.
      headerMap - A map of header parameters.
      contentType - The content type of the request body.
      Returns:
      The response body as a String.
    • post

      public static String post(String url, Map<String,String> formMap, String contentType, String charset)
      Sends a POST request with form data, a specified content type, and a custom encoding.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      contentType - The content type of the request body.
      charset - The custom character set for the request and response.
      Returns:
      The response body as a String.
    • post

      public static String post(String url, Map<String,String> formMap, Map<String,String> headerMap, String contentType)
      Sends a POST request with form data, header parameters, and a specified content type.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      headerMap - A map of header parameters.
      contentType - The content type of the request body.
      Returns:
      The response body as a String.
    • post

      public static String post(String url, Map<String,String> formMap, Map<String,String> headerMap, String contentType, String charset)
      Sends a POST request with form data, header parameters, a specified content type, and a custom encoding.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      headerMap - A map of header parameters.
      contentType - The content type of the request body.
      charset - The custom character set for the request and response.
      Returns:
      The response body as a String.
    • put

      public static String put(String url)
      Sends a simple PUT request with a null body and default UTF-8 encoding.
      Parameters:
      url - The URL to send the request to.
      Returns:
      The response body as a String.
    • put

      public static String put(String url, Map<String,String> formMap)
      Sends a PUT request with form data using application/x-www-form-urlencoded content type.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      Returns:
      The response body as a String.
    • put

      public static String put(String url, String data, String contentType)
      Sends a PUT request with a raw data body and a specified content type.
      Parameters:
      url - The URL to send the request to.
      data - The raw request body data.
      contentType - The content type of the request body.
      Returns:
      The response body as a String.
    • put

      public static String put(String url, Map<String,String> formMap, String contentType)
      Sends a PUT request with form data and a specified content type.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      contentType - The content type of the request body.
      Returns:
      The response body as a String.
    • put

      public static String put(String url, Map<String,String> formMap, Map<String,String> headerMap)
      Sends a PUT request with form data and header parameters.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      headerMap - A map of header parameters.
      Returns:
      The response body as a String.
    • put

      public static String put(String url, String data, String contentType, String charset)
      Sends a PUT request with a raw data body, a specified content type, and a custom encoding.
      Parameters:
      url - The URL to send the request to.
      data - The raw request body data.
      contentType - The content type of the request body.
      charset - The custom character set for the request and response.
      Returns:
      The response body as a String.
    • put

      public static String put(String url, String data, Map<String,String> headerMap, String contentType)
      Sends a PUT request with a raw data body, header parameters, and a specified content type.
      Parameters:
      url - The URL to send the request to.
      data - The raw request body data.
      headerMap - A map of header parameters.
      contentType - The content type of the request body.
      Returns:
      The response body as a String.
    • put

      public static String put(String url, Map<String,String> formMap, String contentType, String charset)
      Sends a PUT request with form data, a specified content type, and a custom encoding.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      contentType - The content type of the request body.
      charset - The custom character set for the request and response.
      Returns:
      The response body as a String.
    • put

      public static String put(String url, Map<String,String> formMap, Map<String,String> headerMap, String contentType)
      Sends a PUT request with form data, header parameters, and a specified content type.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      headerMap - A map of header parameters.
      contentType - The content type of the request body.
      Returns:
      The response body as a String.
    • put

      public static String put(String url, Map<String,String> formMap, Map<String,String> headerMap, String contentType, String charset)
      Sends a PUT request with form data, header parameters, a specified content type, and a custom encoding.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      headerMap - A map of header parameters.
      contentType - The content type of the request body.
      charset - The custom character set for the request and response.
      Returns:
      The response body as a String.
    • delete

      public static String delete(String url)
      Sends a simple DELETE request with default UTF-8 encoding.
      Parameters:
      url - The URL to send the request to.
      Returns:
      The response body as a String.
    • delete

      public static String delete(String url, Map<String,String> formMap)
      Sends a DELETE request with form data and default UTF-8 encoding.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      Returns:
      The response body as a String.
    • delete

      public static String delete(String url, Map<String,String> formMap, String charset)
      Sends a DELETE request with form data and a custom encoding.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      charset - The custom character set for the request and response.
      Returns:
      The response body as a String.
    • delete

      public static String delete(String url, Map<String,String> formMap, Map<String,String> headerMap)
      Sends a DELETE request with form data and header parameters.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      headerMap - A map of header parameters.
      Returns:
      The response body as a String.
    • delete

      public static String delete(String url, Map<String,String> formMap, Map<String,String> headerMap, String charset)
      Sends a DELETE request with form data, header parameters, and a custom encoding.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      headerMap - A map of header parameters.
      charset - The custom character set for the request and response.
      Returns:
      The response body as a String.
    • head

      public static String head(String url)
      Sends a simple HEAD request with default UTF-8 encoding.
      Parameters:
      url - The URL to send the request to.
      Returns:
      The response headers as a formatted string.
    • head

      public static String head(String url, String charset)
      Sends a HEAD request with a custom encoding.
      Parameters:
      url - The URL to send the request to.
      charset - The custom character set.
      Returns:
      The response headers as a formatted string.
    • head

      public static String head(String url, Map<String,String> headerMap, String charset)
      Sends a HEAD request with header parameters and a custom encoding.
      Parameters:
      url - The URL to send the request to.
      headerMap - A map of header parameters.
      charset - The custom character set.
      Returns:
      The response headers as a formatted string.
    • post

      public static String post(String url, Map<String,String> formMap, List<String> list)
      Sends a POST request with form data and file uploads.
      Parameters:
      url - The URL to send the request to.
      formMap - A map of form data.
      list - A list of file paths to upload.
      Returns:
      The response body as a String.