Skip to content

Instantly share code, notes, and snippets.

@cbergau
Created April 24, 2019 11:47
Show Gist options
  • Save cbergau/fe1da7be2a1162d70721c474c2a53cf0 to your computer and use it in GitHub Desktop.
Save cbergau/fe1da7be2a1162d70721c474c2a53cf0 to your computer and use it in GitHub Desktop.
Simple GET Request with Apache HTTP Client
package de.christianbergau;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class HttpClientApp {
public static void main(String... args) {
int timeout = 3;
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeout * 1000)
.setSocketTimeout(timeout * 1000)
.build();
try (CloseableHttpClient client = HttpClientBuilder.create()
.setDefaultRequestConfig(config)
.disableRedirectHandling()
.build()) {
HttpGet request = new HttpGet("https://www.google.de");
HttpResponse response = client.execute(request);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment