Created
February 24, 2017 12:24
-
-
Save bibarsov/ed2f36ab22c9e45cc3c2eabf0a985db0 to your computer and use it in GitHub Desktop.
apache httpclient 4.5 proxy request example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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