Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Usage of Apache Http Client
public final class HttpClientUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientUtils.class);
private static final int CONNECTION_TIMEOUT = 10000;
private HttpClientUtils() {
}
/**
* Performs GET request on requested url and reads the response content from it.
*
* @param url url to get response from
*
* @return http GET response
* @throws IOException if any error occurs
*/
public static ClientResponse proceedGetRequest(final String url) throws IOException {
try (final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(createRequestTimeout()).build()) {
final HttpGet request = new HttpGet(url);
final HttpResponse response = client.execute(request);
final String responseString = HttpClientUtils.readResponseContent(response);
final int responseStatus = response.getStatusLine().getStatusCode();
final ClientResponse clientResponse = new ClientResponse(responseString, responseStatus);
LOGGER.trace("Response : {}", responseString);
LOGGER.debug("Response Code : {}", responseStatus);
return clientResponse;
}
}
/**
* Performs POST request on requested url with form parameters and reads the response content from it.
*
* @param url url to get response from
*
* @return http POST response
* @throws IOException if any error occurs
*/
public static ClientResponse proceedPostRequest(final String url, final List<NameValuePair> formParameters) throws IOException {
try (final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(createRequestTimeout()).build()) {
final HttpPost request = new HttpPost(url);
request.setEntity(new UrlEncodedFormEntity(formParameters));
final HttpResponse response = client.execute(request);
final String responseString = HttpClientUtils.readResponseContent(response);
final int responseStatus = response.getStatusLine().getStatusCode();
final ClientResponse clientResponse = new ClientResponse(responseString, responseStatus);
LOGGER.trace("Response : {}", responseString);
LOGGER.debug("Response Code : {}", responseStatus);
return clientResponse;
}
}
/**
* Performs POST request with json content on requested url and reads the response content from it.
*
* @param url url to get response from
*
* @return http POST response
* @throws IOException if any error occurs
*/
public static ClientResponse proceedPostRequest(final String url, final String json) throws IOException {
try (final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(createRequestTimeout()).build()) {
final HttpPost request = new HttpPost(url);
final HttpEntity entry = new StringEntity(json, ContentType.create("application/json", Charset.forName("UTF-8")));
request.setEntity(entry);
final HttpResponse response = client.execute(request);
final String responseString = HttpClientUtils.readResponseContent(response);
final int responseStatus = response.getStatusLine().getStatusCode();
final ClientResponse clientResponse = new ClientResponse(responseString, responseStatus);
LOGGER.trace("Response : {}", responseString);
LOGGER.debug("Response Code : {}", responseStatus);
return clientResponse;
}
}
private static String readResponseContent(final HttpResponse response) throws IOException {
final StringBuilder result = new StringBuilder();
try (final BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent(), "UTF8"))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
}
return result.toString();
}
private static RequestConfig createRequestTimeout() {
return RequestConfig.custom().setConnectTimeout(CONNECTION_TIMEOUT).setConnectionRequestTimeout(CONNECTION_TIMEOUT).build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment