java.lang.Object
org.miaixz.bus.http.Httpx
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 Summary
ConstructorsConstructorDescriptionHttpx()Default constructor that initializes the client with default timeouts.Httpx(int connTimeout, int readTimeout, int writeTimeout) Constructor that initializes the client with custom timeouts.Httpx(int connTimeout, int readTimeout, int writeTimeout, int maxRequests, int maxRequestsPerHost, int maxIdleConnections, int keepAliveDuration) Constructor that initializes the client with detailed custom settings.Httpx(X509TrustManager x509TrustManager) Constructor that initializes the client with a custom trust manager.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.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 underlyingHttpdclient with all possible configurations. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringSends a simple DELETE request with default UTF-8 encoding.static StringSends a DELETE request with form data and default UTF-8 encoding.static StringSends a DELETE request with form data and a custom encoding.static StringSends a DELETE request with form data and header parameters.static StringSends a DELETE request with form data, header parameters, and a custom encoding.static StringSends a simple GET request with the default UTF-8 encoding.static StringSends a GET request, either synchronously or asynchronously.static StringSends a simple GET request with a custom encoding.static StringSends a GET request with query parameters and default UTF-8 encoding.static StringSends a GET request with query and header parameters and default UTF-8 encoding.static StringSends a GET request with query parameters, header parameters, and a custom encoding.static voidSends an asynchronous GET request with a callback.static StringSends a simple HEAD request with default UTF-8 encoding.static StringSends a HEAD request with a custom encoding.static StringSends a HEAD request with header parameters and a custom encoding.static StringSends a simple POST request with an empty body.static StringSends a POST request with a raw data body and a specified content type.static StringSends a POST request with a raw data body, a specified content type, and a custom encoding.static StringSends a POST request with a raw data body, header parameters, and a specified content type.static StringSends a POST request with form data usingapplication/x-www-form-urlencodedcontent type.static StringSends a POST request with form data and a specified content type.static StringSends a POST request with form data, a specified content type, and a custom encoding.static StringSends a POST request with form data and file uploads.static StringSends a POST request with form data and header parameters.static StringSends a POST request with form data, header parameters, and a specified content type.static Stringpost(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.static voidSends an asynchronous POST request with form data and a callback.static StringSends a simple PUT request with a null body and default UTF-8 encoding.static StringSends a PUT request with a raw data body and a specified content type.static StringSends a PUT request with a raw data body, a specified content type, and a custom encoding.static StringSends a PUT request with a raw data body, header parameters, and a specified content type.static StringSends a PUT request with form data usingapplication/x-www-form-urlencodedcontent type.static StringSends a PUT request with form data and a specified content type.static StringSends a PUT request with form data, a specified content type, and a custom encoding.static StringSends a PUT request with form data and header parameters.static StringSends a PUT request with form data, header parameters, and a specified content type.static Stringput(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.
-
Constructor Details
-
Httpx
public Httpx()Default constructor that initializes the client with default timeouts. -
Httpx
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 underlyingHttpdclient 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
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
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
Sends a GET request, either synchronously or asynchronously.- Parameters:
url- The URL to send the request to.isAsync- Iftrue, the request is sent asynchronously.- Returns:
- The response body as a
String(for synchronous requests) or an empty string (for asynchronous requests).
-
get
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
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
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
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
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
Sends a POST request with form data usingapplication/x-www-form-urlencodedcontent type.- Parameters:
url- The URL to send the request to.formMap- A map of form data.- Returns:
- The response body as a
String.
-
post
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
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
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
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
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
Sends a PUT request with form data usingapplication/x-www-form-urlencodedcontent type.- Parameters:
url- The URL to send the request to.formMap- A map of form data.- Returns:
- The response body as a
String.
-
put
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
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
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
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
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
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
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
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
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
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
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
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
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.
-