Skip to content

Instantly share code, notes, and snippets.

@ehartmann
Created June 6, 2014 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ehartmann/f5b9cb64d585496f6e49 to your computer and use it in GitHub Desktop.
Save ehartmann/f5b9cb64d585496f6e49 to your computer and use it in GitHub Desktop.
Jenkins with http commons
URL jenkinsURL = new URL(config.getJenkinsUrl());
// Create AuthCache instance
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
new AuthScope(jenkinsURL.getHost(), jenkinsURL.getPort(), "realm"),
new UsernamePasswordCredentials(config.getJenkinsUsername(), config.getJenkinsPassword())
);
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(new HttpHost(jenkinsURL.getHost()), basicAuth);
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsProvider);
context.setAuthCache(authCache);
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
HttpPost post = new HttpPost(Configuration.getConfig().getJenkinsUrl() + "script/");
List<NameValuePair> nvps = new ArrayList<>();
nvps.add(new BasicNameValuePair("script", nodeConfig.getGroovyScript()));
post.setEntity(new UrlEncodedFormEntity(nvps));
try {
HttpResponse response = httpclient.execute(post, context);
LOG.warn(IOUtils.toString(response.getEntity().getContent()));
} catch (HttpResponseException ex) {
LOG.error("Http Error", ex);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment