Skip to content

Instantly share code, notes, and snippets.

@LenarBad
Created February 2, 2018 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LenarBad/224751f163781594b2dfb135535fda04 to your computer and use it in GitHub Desktop.
Save LenarBad/224751f163781594b2dfb135535fda04 to your computer and use it in GitHub Desktop.
How to send GET Request and get Response with Apache Http Components
String url = "https://www.some-web-service/api/endpoint";
HttpGet get = new HttpGet(url);
Header headers[] = { new BasicHeader("Accept", "application/json")};
get.setHeaders(headers);
CloseableHttpClient client = HttpClients.custom().build();
HttpResponse response = client.execute(get);
String result = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
int responseCode = response.getStatusLine().getStatusCode();
String statusPhrase = response.getStatusLine().getReasonPhrase();
response.getEntity().getContent().close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment