Created
December 12, 2016 23:49
-
-
Save ataylor284/7e6c07c4b9a6d9bceb1d60a75b070ede to your computer and use it in GitHub Desktop.
http proxy credentials
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
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.5.2') | |
import org.apache.http.* | |
import org.apache.http.auth.* | |
import org.apache.http.client.* | |
import org.apache.http.client.config.* | |
import org.apache.http.client.methods.* | |
import org.apache.http.impl.auth.* | |
import org.apache.http.impl.client.* | |
import org.apache.http.util.* | |
import org.apache.http.client.protocol.* | |
def credsProvider = new BasicCredentialsProvider(); | |
def proxy = new HttpHost('localhost', 8888) | |
def target = new HttpHost('localhost', 8081) | |
AuthCache authCache = new BasicAuthCache() | |
authCache.put(proxy, new BasicScheme(ChallengeState.PROXY)) | |
credsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials('test', 'test')) | |
HttpClientContext context = HttpClientContext.create() | |
context.setCredentialsProvider(credsProvider) | |
context.setAuthCache(authCache) | |
CloseableHttpClient httpclient = HttpClients.custom() | |
.setDefaultCredentialsProvider(credsProvider) | |
.build(); | |
RequestConfig config = RequestConfig.custom() | |
.setProxy(proxy) | |
.build(); | |
HttpGet httpget = new HttpGet('/'); | |
httpget.setConfig(config); | |
CloseableHttpResponse response = httpclient.execute(target, httpget, context); | |
println(response.getStatusLine()); | |
//println(EntityUtils.toString(response.getEntity())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment