Skip to content

Instantly share code, notes, and snippets.

@fuxingloh
Last active August 29, 2015 14:24
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 fuxingloh/0d01df63cfd7f0ada446 to your computer and use it in GitHub Desktop.
Save fuxingloh/0d01df63cfd7f0ada446 to your computer and use it in GitHub Desktop.
Http Get/Post with Apache Utils
default String connect(String url) throws IOException {
try (CloseableHttpClient client = HttpClients.createDefault()){
// Create http post
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
return EntityUtils.toString(response.getEntity());
}
}
public static String makeRequest() throws IOException {
try (CloseableHttpClient client = HttpClients.createDefault()){
// Create http post
HttpPost post = new HttpPost("http://");
// Create post string
post.setEntity(new StringEntity("something"));
HttpResponse response = client.execute(post);
return EntityUtils.toString(response.getEntity());
}
}
default String connect(String url, Header[] headers) throws IOException {
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(TIMEOUT)
.setConnectTimeout(TIMEOUT)
.build();
try (CloseableHttpClient client = HttpClientBuilder.create()
.setUserAgent(USER_AGENT)
.setDefaultRequestConfig(requestConfig).build()){
// Create http post
HttpGet get = new HttpGet(url);
get.setHeaders(headers);
HttpResponse response = client.execute(get);
return EntityUtils.toString(response.getEntity());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment