java.lang.Object
org.seppiko.commons.utils.http.HttpClientUtil
- Direct Known Subclasses:
AsyncHttpClientUtil
Http Client Util with java.net.http
- Author:
- Leonard Woo
-
Method Summary
Modifier and TypeMethodDescriptionprotected static <T> HttpResponse.BodyHandler<?>getBodyHandler(Class<T> type) Response body checkerprotected static <T> HttpRequest.BodyPublishergetBodyPublisher(T requestBody) Request body checkerstatic HttpRequestgetRequest(String url, HttpMethod method, int timeout, HttpHeaders headers, byte[] requestBody) Get Request instance with byte arraystatic HttpRequestgetRequest(String url, HttpMethod method, int timeout, HttpHeaders headers, InputStream requestBody) Get Request instance with InputStreamstatic HttpRequestgetRequest(String url, HttpMethod method, int timeout, HttpHeaders headers, String requestBody) Get Request instance with Stringprotected static HttpRequestgetRequestRaw(URI uri, HttpMethod method, int timeout, String[] headers, HttpRequest.BodyPublisher requestBody) Get HTTP request rawstatic HttpResponse<byte[]>getResponseByteArray(HttpRequest req, SSLContext sslContext, InetSocketAddress proxy) Get Response instance with byte array bodystatic HttpResponse<InputStream>getResponseInputStream(HttpRequest req, SSLContext sslContext, InetSocketAddress proxy) Get Response instance with InputStream bodyprotected static <T> HttpResponse<T>getResponseRaw(HttpRequest req, HttpResponse.BodyHandler<T> responseBodyHandler, SSLContext sslContext, InetSocketAddress proxy) Get HTTP response rawstatic HttpResponse<String>getResponseString(HttpRequest req, SSLContext sslContext, InetSocketAddress proxy) Get Response instance with String body
-
Method Details
-
getRequest
public static HttpRequest getRequest(String url, HttpMethod method, int timeout, HttpHeaders headers, String requestBody) throws URISyntaxException, IllegalArgumentException, NullPointerException, HttpRuntimeException Get Request instance with String- Parameters:
url- URL String.method- seeHttpMethod.timeout- Timeout.headers- seeHttpHeaders.requestBody- Request body string.- Returns:
- HttpRequest instance.
- Throws:
URISyntaxException- URL is an illegal address.IllegalArgumentException- something not supported or undefined.NullPointerException- something isnull.HttpRuntimeException- Http request process exception.
-
getRequest
public static HttpRequest getRequest(String url, HttpMethod method, int timeout, HttpHeaders headers, InputStream requestBody) throws URISyntaxException, IllegalArgumentException, NullPointerException, HttpRuntimeException Get Request instance with InputStream- Parameters:
url- URL String.method- seeHttpMethod.timeout- Timeout.headers- seeHttpHeaders.requestBody- Request body input stream.- Returns:
- HttpRequest instance.
- Throws:
URISyntaxException- URL is an illegal address.IllegalArgumentException- something not supported or undefined.NullPointerException- something isnull.HttpRuntimeException- Http request process exception.
-
getRequest
public static HttpRequest getRequest(String url, HttpMethod method, int timeout, HttpHeaders headers, byte[] requestBody) throws URISyntaxException, IllegalArgumentException, NullPointerException, HttpRuntimeException Get Request instance with byte array- Parameters:
url- URL String.method- seeHttpMethod.timeout- Timeout.headers- seeHttpHeaders.requestBody- Request body byte array.- Returns:
- HttpRequest instance.
- Throws:
URISyntaxException- URL is an illegal address.IllegalArgumentException- something not supported or undefined.NullPointerException- something isnull.HttpRuntimeException- Http request process exception.
-
getResponseString
public static HttpResponse<String> getResponseString(HttpRequest req, SSLContext sslContext, InetSocketAddress proxy) throws HttpClientException, HttpInterruptedException Get Response instance with String body- Parameters:
req- HttpRequest instance.sslContext- HTTP TLS Context, if unused setTLSUtil.NULL_SSL_CONTEXT. seeTLSUtil.proxy- Http proxy, Notnullis enabled proxy.- Returns:
- HttpResponse object, if NULL has failed.
- Throws:
HttpClientException- get response exception.HttpInterruptedException- if the operation is interrupted
-
getResponseByteArray
public static HttpResponse<byte[]> getResponseByteArray(HttpRequest req, SSLContext sslContext, InetSocketAddress proxy) throws HttpClientException, HttpInterruptedException Get Response instance with byte array body- Parameters:
req- HttpRequest instance.sslContext- HTTP TLS Context, if unused setTLSUtil.NULL_SSL_CONTEXT. seeTLSUtil.proxy- Http proxy, Notnullis enabled proxy.- Returns:
- HttpResponse object, if NULL has failed.
- Throws:
HttpClientException- get response exception.HttpInterruptedException- if the operation is interrupted.
-
getResponseInputStream
public static HttpResponse<InputStream> getResponseInputStream(HttpRequest req, SSLContext sslContext, InetSocketAddress proxy) throws HttpClientException, HttpInterruptedException Get Response instance with InputStream body- Parameters:
req- HttpRequest instance.sslContext- HTTP TLS Context, if unused setTLSUtil.NULL_SSL_CONTEXT. seeTLSUtil.proxy- Http proxy, Notnullis enabled proxy.- Returns:
- HttpResponse object, if NULL has failed.
- Throws:
HttpClientException- get response exception.HttpInterruptedException- if the operation is interrupted.
-
getRequestRaw
protected static HttpRequest getRequestRaw(URI uri, HttpMethod method, int timeout, String[] headers, HttpRequest.BodyPublisher requestBody) throws IllegalArgumentException, IllegalStateException, HttpTimeoutException Get HTTP request raw- Parameters:
uri- request uri string.method- request method, seeHttpMethod.timeout- request timeout.headers- request headers, seeHttpHeaders, if none isnull.requestBody- request body.- Returns:
- HttpRequest object
- Throws:
IllegalArgumentException- URI method or headers is not support or validIllegalStateException- if a URI has not been setHttpTimeoutException- timeout is not valid
-
getResponseRaw
protected static <T> HttpResponse<T> getResponseRaw(HttpRequest req, HttpResponse.BodyHandler<T> responseBodyHandler, SSLContext sslContext, InetSocketAddress proxy) throws HttpInterruptedException, HttpResponseException, SecurityException Get HTTP response raw- Type Parameters:
T- Response content type.- Parameters:
req- HTTP requestresponseBodyHandler- Response body content type.sslContext- HTTP TLS Context, if unused setTLSUtil.NULL_SSL_CONTEXT. seeTLSUtil.proxy- HTTP Proxy, Not null is enabled proxy.- Returns:
- Http response instance.
- Throws:
HttpInterruptedException- If the response is interrupted.HttpResponseException- If the response has I/O error or not valid.SecurityException- If a security manager has been installed, and it denies access to the URL in the given request, or proxy if one is configured.
-
getBodyPublisher
protected static <T> HttpRequest.BodyPublisher getBodyPublisher(T requestBody) throws UnsupportedOperationException Request body checker- Type Parameters:
T- body type.- Parameters:
requestBody- request body.- Returns:
- BodyPublisher instance.
- Throws:
UnsupportedOperationException- not found request body class type
-
getBodyHandler
Response body checker- Type Parameters:
T- class type.- Parameters:
type- response type.- Returns:
- BodyHandler instance.
-