Skip to content

Instantly share code, notes, and snippets.

@ataylor284
Created December 12, 2016 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ataylor284/7e6c07c4b9a6d9bceb1d60a75b070ede to your computer and use it in GitHub Desktop.
Save ataylor284/7e6c07c4b9a6d9bceb1d60a75b070ede to your computer and use it in GitHub Desktop.
http proxy credentials
@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