Skip to content

Instantly share code, notes, and snippets.

@bibarsov
Created February 24, 2017 12:24
Show Gist options
  • Save bibarsov/ed2f36ab22c9e45cc3c2eabf0a985db0 to your computer and use it in GitHub Desktop.
Save bibarsov/ed2f36ab22c9e45cc3c2eabf0a985db0 to your computer and use it in GitHub Desktop.
apache httpclient 4.5 proxy request example
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("proxy host", 3128),
new UsernamePasswordCredentials("login", "password"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
try {
HttpHost target = new HttpHost("root target url", 443, "https");
HttpHost proxy = new HttpHost("proxy host", 3128);
RequestConfig config = RequestConfig.custom()
.setProxy(proxy)
.build();
HttpGet httpget = new HttpGet("/path");
httpget.setConfig(config);
System.out.println("Executing request " + httpget.getRequestLine() + " to " + target + " via " + proxy);
CloseableHttpResponse response = httpclient.execute(target, httpget);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
} finally {
response.close();
}
} finally {
httpclient.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment