java.lang.Object
org.seppiko.commons.utils.http.HttpClientUtil
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 object with byte arraystatic HttpRequestgetRequest(String url, HttpMethod method, int timeout, HttpHeaders headers, InputStream requestBody) Get Request object with inputstreamstatic HttpRequestgetRequest(String url, HttpMethod method, int timeout, HttpHeaders headers, String requestBody) Get Request object with Stringprotected static HttpRequestgetRequestRaw(URI uri, HttpMethod method, int timeout, String[] headers, HttpRequest.BodyPublisher requestBody) Get HTTP request rawstatic HttpResponse<byte[]>getResponseByteArray(HttpRequest req, InetSocketAddress proxy) Get Response with byte array bodystatic HttpResponse<InputStream>getResponseInputStream(HttpRequest req, InetSocketAddress proxy) Get Response with inputstream bodystatic HttpResponse<String>getResponseString(HttpRequest req, InetSocketAddress proxy) Get Response with String bodystatic <R,T> HttpResponse<T> sendRequest(String url, HttpMethod method, int timeout, HttpHeaders headers, R requestBody, Class<T> responseType, InetSocketAddress proxy) send http request
-
Method Details
-
getRequest
public static HttpRequest getRequest(String url, HttpMethod method, int timeout, HttpHeaders headers, String requestBody) throws URISyntaxException, IllegalArgumentException, NullPointerException, HttpRuntimeException Get Request object with String- Parameters:
url- URLmethod- seeHttpMethodtimeout- Timeoutheaders- HeadersrequestBody- Request body string- Returns:
- HttpRequest object
- Throws:
URISyntaxException- URL is an illegal addressIllegalArgumentException- something not supported or undefinedNullPointerException- something is nullHttpRuntimeException- 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 object with inputstream- Parameters:
url- URLmethod- seeHttpMethodtimeout- Timeoutheaders- HeadersrequestBody- Request body inputstream- Returns:
- HttpRequest object
- Throws:
URISyntaxException- URL is an illegal addressIllegalArgumentException- something not supported or undefinedNullPointerException- something is nullHttpRuntimeException- 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 object with byte array- Parameters:
url- URLmethod- seeHttpMethodtimeout- Timeoutheaders- HeadersrequestBody- Request body byte array- Returns:
- HttpRequest object
- Throws:
URISyntaxException- URL is an illegal addressIllegalArgumentException- something not supported or undefinedNullPointerException- something is nullHttpRuntimeException- Http request process exception
-
getResponseString
public static HttpResponse<String> getResponseString(HttpRequest req, InetSocketAddress proxy) throws HttpClientException, HttpInterruptedException Get Response with String body- Parameters:
req- HttpRequest objectproxy- Http proxy, Not null is enabled proxy- Returns:
- HttpResponse object, if NULL has failed
- Throws:
HttpClientException- get response exceptionHttpInterruptedException- if the operation is interrupted
-
getResponseByteArray
public static HttpResponse<byte[]> getResponseByteArray(HttpRequest req, InetSocketAddress proxy) throws HttpClientException, HttpInterruptedException Get Response with byte array body- Parameters:
req- HttpRequest objectproxy- Http proxy, Not null is enabled proxy- Returns:
- HttpResponse object, if NULL has failed
- Throws:
HttpClientException- get response exceptionHttpInterruptedException- if the operation is interrupted
-
getResponseInputStream
public static HttpResponse<InputStream> getResponseInputStream(HttpRequest req, InetSocketAddress proxy) throws HttpClientException, HttpInterruptedException Get Response with inputstream body- Parameters:
req- HttpRequest objectproxy- Http proxy, Not null is enabled proxy- Returns:
- HttpResponse object, if NULL has failed
- Throws:
HttpClientException- get response exceptionHttpInterruptedException- if the operation is interrupted
-
sendRequest
public static <R,T> HttpResponse<T> sendRequest(String url, HttpMethod method, int timeout, HttpHeaders headers, R requestBody, Class<T> responseType, InetSocketAddress proxy) throws URISyntaxException, HttpClientException, HttpRuntimeException, HttpTimeoutException send http request- Type Parameters:
R- request type (String, InputStream and byte[])T- response type (String, InputStream and byte[])- Parameters:
url- URLmethod- request http methodtimeout- request timeoutheaders- request headers, if none is nullrequestBody- request body, is none is nullresponseType- response type, default is byte arrayproxy- request http proxy, if had not is null- Returns:
- http response
- Throws:
URISyntaxException- URL is an illegal addressHttpClientException- get response exceptionHttpRuntimeException- http request failed or the operation is interruptedHttpTimeoutException- http request timeout
-
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 urimethod- request methodtimeout- request timeoutheaders- request headersrequestBody- 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
-
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 object
- Throws:
UnsupportedOperationException- not found request body class type
-
getBodyHandler
Response body checker- Type Parameters:
T- class type- Parameters:
type- response type- Returns:
- BodyHandler type
-