Package net.lenni0451.commons.httpclient
Class HttpClient
- java.lang.Object
-
- net.lenni0451.commons.httpclient.HeaderStore<HttpClient>
-
- net.lenni0451.commons.httpclient.HttpClient
-
- All Implemented Interfaces:
HttpRequestBuilder
public class HttpClient extends HeaderStore<HttpClient> implements HttpRequestBuilder
-
-
Constructor Summary
Constructors Constructor Description HttpClient()Create a new http client with the default executor.HttpClient(java.util.function.Function<HttpClient,RequestExecutor> executorSupplier)Create a new http client with the given executor.
You'll only need this if you want to use a custom executor.HttpClient(ExecutorType executorType)Create a new http client with the given executor type.
Some executor types may not be supported by your Java version.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <T extends HttpRequest>
Tbind(T request)Bind the given request to a client.HttpResponseexecute(HttpRequest request)Execute a request and return the response.<R> Rexecute(HttpRequest request, HttpResponseHandler<R> responseHandler)Execute a request and pass the response to the response handler.
The return value of the response handler will be returned.<T extends HttpRequest & HttpResponseHandler<R>,R>
RexecuteAndHandle(T requestAndHandler)Execute a request that is also a response handler and return the handled response.
This is just a shortcut forexecute(HttpRequest, HttpResponseHandler).intgetConnectTimeout()java.net.CookieManagergetCookieManager()ProxyHandlergetProxyHandler()intgetReadTimeout()RetryConfiggetRetryHandler()booleanisFollowRedirects()booleanisIgnoreInvalidSSL()HttpClientsetConnectTimeout(int connectTimeout)Set the connect timeout for all requests.HttpClientsetCookieManager(java.net.CookieManager cookieManager)Set the cookie manager to use for all requests.
If this is null no cookies will be used.HttpClientsetExecutor(java.util.function.Function<HttpClient,RequestExecutor> executorSupplier)Set the executor to use for all requests.HttpClientsetFollowRedirects(boolean followRedirects)Set whether redirects should be followed.HttpClientsetIgnoreInvalidSSL(boolean ignoreInvalidSSL)Set whether invalid SSL certificates should be ignored.HttpClientsetProxyHandler(ProxyHandler proxyHandler)Set the proxy handler for all requests.HttpClientsetReadTimeout(int readTimeout)Set the read timeout for all requests.HttpClientsetRetryConfig(RetryConfig retryConfig)Set the retry handler for all requests.HttpClientsetRetryHandler(RetryConfig retryConfig)Deprecated.java.lang.StringtoString()-
Methods inherited from class net.lenni0451.commons.httpclient.HeaderStore
appendHeader, appendHeader, appendHeader, clearHeaders, forEachHeader, getFirstHeader, getHeader, getHeaders, getLastHeader, hasHeader, hasHeader, hasHeader, removeHeader, setHeader, setHeader, setHeader
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface net.lenni0451.commons.httpclient.HttpRequestBuilder
contentRequest, contentRequest, delete, delete, get, get, head, head, post, post, put, put, request, request
-
-
-
-
Constructor Detail
-
HttpClient
public HttpClient()
Create a new http client with the default executor.
-
HttpClient
public HttpClient(@Nonnull ExecutorType executorType)Create a new http client with the given executor type.
Some executor types may not be supported by your Java version. Make sure to checkExecutorType.isAvailable()before using them.- Parameters:
executorType- The executor type to use
-
HttpClient
public HttpClient(@Nonnull java.util.function.Function<HttpClient,RequestExecutor> executorSupplier)Create a new http client with the given executor.
You'll only need this if you want to use a custom executor. For using an official executor useExecutorTypewithHttpClient(ExecutorType).- Parameters:
executorSupplier- The supplier for the executor to use
-
-
Method Detail
-
setExecutor
public HttpClient setExecutor(@Nonnull java.util.function.Function<HttpClient,RequestExecutor> executorSupplier)
Set the executor to use for all requests.- Parameters:
executorSupplier- The supplier for the executor to use- Returns:
- This instance for chaining
-
getCookieManager
@Nullable public java.net.CookieManager getCookieManager()
- Returns:
- The cookie manager
-
setCookieManager
public HttpClient setCookieManager(@Nullable java.net.CookieManager cookieManager)
Set the cookie manager to use for all requests.
If this is null no cookies will be used.- Parameters:
cookieManager- The cookie manager to use- Returns:
- This instance for chaining
-
isFollowRedirects
public boolean isFollowRedirects()
- Returns:
- Whether redirects should be followed
-
setFollowRedirects
public HttpClient setFollowRedirects(boolean followRedirects)
Set whether redirects should be followed.- Parameters:
followRedirects- Whether redirects should be followed- Returns:
- This instance for chaining
-
getConnectTimeout
public int getConnectTimeout()
- Returns:
- The connect timeout for all requests in milliseconds
-
setConnectTimeout
public HttpClient setConnectTimeout(int connectTimeout)
Set the connect timeout for all requests.- Parameters:
connectTimeout- The connect timeout in milliseconds- Returns:
- This instance for chaining
-
getReadTimeout
public int getReadTimeout()
- Returns:
- The read timeout for all requests in milliseconds
-
setReadTimeout
public HttpClient setReadTimeout(int readTimeout)
Set the read timeout for all requests.- Parameters:
readTimeout- The read timeout in milliseconds- Returns:
- This instance for chaining
-
getRetryHandler
@Nonnull public RetryConfig getRetryHandler()
- Returns:
- The retry handler
-
setRetryConfig
public HttpClient setRetryConfig(@Nonnull RetryConfig retryConfig)
Set the retry handler for all requests.- Parameters:
retryConfig- The retry handler- Returns:
- This instance for chaining
-
setRetryHandler
@Deprecated @ScheduledForRemoval public HttpClient setRetryHandler(@Nonnull RetryConfig retryConfig)
Deprecated.Deprecated, usesetRetryConfig(RetryConfig)instead.
-
getProxyHandler
@Nonnull public ProxyHandler getProxyHandler()
- Returns:
- The proxy handler
-
setProxyHandler
public HttpClient setProxyHandler(@Nonnull ProxyHandler proxyHandler)
Set the proxy handler for all requests.- Parameters:
proxyHandler- The proxy handler- Returns:
- This instance for chaining
-
isIgnoreInvalidSSL
public boolean isIgnoreInvalidSSL()
- Returns:
- Whether invalid SSL certificates should be ignored
-
setIgnoreInvalidSSL
public HttpClient setIgnoreInvalidSSL(boolean ignoreInvalidSSL)
Set whether invalid SSL certificates should be ignored.- Parameters:
ignoreInvalidSSL- Whether invalid SSL certificates should be ignored- Returns:
- This instance for chaining
-
executeAndHandle
public <T extends HttpRequest & HttpResponseHandler<R>,R> R executeAndHandle(T requestAndHandler) throws java.io.IOException
Execute a request that is also a response handler and return the handled response.
This is just a shortcut forexecute(HttpRequest, HttpResponseHandler).- Type Parameters:
T- The type of the request and response handlerR- The return type of the response handler- Parameters:
requestAndHandler- The request that is also the response handler- Returns:
- The handled response
- Throws:
java.io.IOException- If an I/O error occurs
-
execute
public <R> R execute(HttpRequest request, HttpResponseHandler<R> responseHandler) throws java.io.IOException
Execute a request and pass the response to the response handler.
The return value of the response handler will be returned.- Type Parameters:
R- The return type of the response handler- Parameters:
request- The request to executeresponseHandler- The response handler- Returns:
- The return value of the response handler
- Throws:
java.io.IOException- If an I/O error occurs
-
execute
public HttpResponse execute(HttpRequest request) throws java.io.IOException
Execute a request and return the response.- Parameters:
request- The request to execute- Returns:
- The response
- Throws:
java.io.IOException- If an I/O error occursRetryExceededException- If the maximum header retry count was exceededjava.lang.IllegalStateException- If the maximum retry count was exceeded but no exception was thrown
-
bind
public <T extends HttpRequest> T bind(T request)
Description copied from interface:HttpRequestBuilderBind the given request to a client.- Specified by:
bindin interfaceHttpRequestBuilder- Type Parameters:
T- The type of the request- Parameters:
request- The request to bind- Returns:
- The bound request
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-