public class HttpClientWrapper
extends java.lang.Object
implements java.io.Closeable
HttpClient instance
for usage with BasicAuthentication user/pwd and also
disables SSL verification to work with self-signed SSL certificates
on webpages.| Constructor and Description |
|---|
HttpClientWrapper(java.lang.String user,
java.lang.String password,
int timeoutMs)
Construct the
HttpClient with the given authentication values
and all timeouts set to the given number of milliseconds |
| Modifier and Type | Method and Description |
|---|---|
static org.apache.http.HttpEntity |
checkAndFetch(org.apache.http.HttpResponse response,
java.lang.String url)
Helper method to check the status code of the response and throw an IOException if it is
an error or moved state.
|
void |
close() |
org.apache.http.impl.client.CloseableHttpClient |
getHttpClient()
Return the current
HttpClient instance. |
static java.lang.String |
retrieveData(java.lang.String url)
Small helper method to simply query the URL without password and
return the resulting data.
|
static java.lang.String |
retrieveData(java.lang.String url,
java.lang.String user,
java.lang.String password,
int timeoutMs)
Small helper method to simply query the URL without password and
return the resulting data.
|
java.lang.String |
simpleGet(java.lang.String url)
Perform a simple get-operation and return the resulting String.
|
void |
simpleGet(java.lang.String url,
java.util.function.Consumer<java.io.InputStream> consumer)
Perform a simple get-operation and passes the resulting InputStream to the given Consumer
|
byte[] |
simpleGetBytes(java.lang.String url)
Perform a simple get-operation and return the resulting byte-array.
|
public HttpClientWrapper(java.lang.String user,
java.lang.String password,
int timeoutMs)
HttpClient with the given authentication values
and all timeouts set to the given number of millisecondsuser - The username for basic authentication, use an empty string when no authentication is requiredpassword - The password for basic authentication, null when no authentication is requiredtimeoutMs - The timeout for socket connection and reading, specified in millisecondspublic org.apache.http.impl.client.CloseableHttpClient getHttpClient()
HttpClient instance.HttpClientpublic java.lang.String simpleGet(java.lang.String url)
throws java.io.IOException
url - The URL to queryjava.io.IOException - if the HTTP status code is not 200.public byte[] simpleGetBytes(java.lang.String url)
throws java.io.IOException
url - The URL to queryjava.io.IOException - if the HTTP status code is not 200.public void simpleGet(java.lang.String url,
java.util.function.Consumer<java.io.InputStream> consumer)
throws java.io.IOException
url - The URL to queryconsumer - A Consumer which receives the InputStream and can process the data
on-the-fly in streaming fashion without retrieving all of the data into memory
at once.java.io.IOException - if the HTTP status code is not 200.public void close()
throws java.io.IOException
close in interface java.io.Closeableclose in interface java.lang.AutoCloseablejava.io.IOExceptionpublic static java.lang.String retrieveData(java.lang.String url)
throws java.io.IOException
url - The URL to query data from.java.io.IOException - If the URL is not accessible or the query returns
a HTTP code other than 200.public static java.lang.String retrieveData(java.lang.String url,
java.lang.String user,
java.lang.String password,
int timeoutMs)
throws java.io.IOException
url - The URL to query data from.user - The username to sendpassword - The password to sendtimeoutMs - How long in milliseconds to wait for the requestjava.io.IOException - If the URL is not accessible or the query returns
a HTTP code other than 200.public static org.apache.http.HttpEntity checkAndFetch(org.apache.http.HttpResponse response,
java.lang.String url)
throws java.io.IOException
response - A HttpResponse that is resulting from executing a HttpMethod.url - The url, only used for building the error message of the exception.HttpEntity returned from response.getEntity().java.io.IOException - if the HTTP status code is higher than 206.