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 Modifier and Type Method Description 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.intgetConnectTimeout()java.net.CookieManagergetCookieManager()ProxyHandlergetProxyHandler()intgetReadTimeout()RetryHandlergetRetryHandler()booleanisFollowRedirects()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.voidsetProxyHandler(ProxyHandler proxyHandler)Set the proxy handler for all requests.HttpClientsetReadTimeout(int readTimeout)Set the read timeout for all requests.HttpClientsetRetryHandler(RetryHandler retryHandler)Set the retry handler for all requests.-
Methods inherited from class net.lenni0451.commons.httpclient.HeaderStore
appendHeader, appendHeader, appendHeader, clearHeaders, 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, toString, 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
-
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 RetryHandler getRetryHandler()
- Returns:
- The retry handler
-
setRetryHandler
public HttpClient setRetryHandler(@Nonnull RetryHandler retryHandler)
Set the retry handler for all requests.- Parameters:
retryHandler- The retry handler- Returns:
- This instance for chaining
-
getProxyHandler
@Nonnull public ProxyHandler getProxyHandler()
- Returns:
- The proxy handler
-
setProxyHandler
public void setProxyHandler(@Nonnull ProxyHandler proxyHandler)Set the proxy handler for all requests.- Parameters:
proxyHandler- The proxy handler
-
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
-
-