java.lang.Object
org.seppiko.commons.utils.http.HttpClientUtil
Http Client Util with java.net.http
- Author:
- Leonard Woo
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic HttpRequestgetRequest(String url, HttpMethod method, int timeout, LinkedHashMap<String,ArrayList<String>> headers, byte[] requestBody)Get Request object with byte arraystatic HttpRequestgetRequest(String url, HttpMethod method, int timeout, LinkedHashMap<String,ArrayList<String>> headers, InputStream requestBody)Get Request object with inputstreamstatic HttpRequestgetRequest(String url, HttpMethod method, int timeout, LinkedHashMap<String,ArrayList<String>> headers, String requestBody)Get Request object with Stringstatic 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<?>sendRequest(String url, HttpMethod method, int timeout, LinkedHashMap<String,ArrayList<String>> headers, R requestBody, Class<T> responseType, InetSocketAddress proxy)send http request
-
Constructor Details
-
HttpClientUtil
public HttpClientUtil()
-
-
Method Details
-
getRequest
public static HttpRequest getRequest(String url, HttpMethod method, int timeout, LinkedHashMap<String,ArrayList<String>> headers, String requestBody) throws URISyntaxException, IllegalArgumentException, NullPointerExceptionGet 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 null
-
getRequest
public static HttpRequest getRequest(String url, HttpMethod method, int timeout, LinkedHashMap<String,ArrayList<String>> headers, InputStream requestBody) throws URISyntaxExceptionGet Request object with inputstream- Parameters:
url- URLmethod- seeHttpMethodtimeout- Timeoutheaders- HeadersrequestBody- Request body inputstream- Returns:
- HttpRequest object
- Throws:
URISyntaxException- URL is an illegal address
-
getRequest
public static HttpRequest getRequest(String url, HttpMethod method, int timeout, LinkedHashMap<String,ArrayList<String>> headers, byte[] requestBody) throws URISyntaxExceptionGet Request object with byte array- Parameters:
url- URLmethod- seeHttpMethodtimeout- Timeoutheaders- HeadersrequestBody- Request body byte array- Returns:
- HttpRequest object
- Throws:
URISyntaxException- URL is an illegal address
-
getResponseString
public static HttpResponse<String> getResponseString(HttpRequest req, InetSocketAddress proxy) throws HttpClientExceptionGet Response with String body- Parameters:
req- HttpRequest objectproxy- Not null is enable proxy- Returns:
- HttpResponse object, if NULL has failed
- Throws:
HttpClientException- get response exception
-
getResponseByteArray
public static HttpResponse<byte[]> getResponseByteArray(HttpRequest req, InetSocketAddress proxy) throws HttpClientExceptionGet Response with byte array body- Parameters:
req- HttpRequest objectproxy- Not null is enable proxy- Returns:
- HttpResponse object, if NULL has failed
- Throws:
HttpClientException- get response exception
-
getResponseInputStream
public static HttpResponse<InputStream> getResponseInputStream(HttpRequest req, InetSocketAddress proxy) throws HttpClientExceptionGet Response with inputstream body- Parameters:
req- HttpRequest objectproxy- Not null is enable proxy- Returns:
- HttpResponse object, if NULL has failed
- Throws:
HttpClientException- get response exception
-
sendRequest
public static <R, T> HttpResponse<?> sendRequest(String url, HttpMethod method, int timeout, LinkedHashMap<String,ArrayList<String>> headers, R requestBody, Class<T> responseType, InetSocketAddress proxy) throws URISyntaxException, HttpClientExceptionsend 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 proxy, if had not is null- Returns:
- http response
- Throws:
URISyntaxException- URL is an illegal addressHttpClientException- get response exception
-