Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.