public static String httpclientGet(String jenkinsUser, String jenkinsPass, String pduName,String jobName,String type)
throws ClientProtocolException, IOException {
String jenkinsUrl="";
if(type=="createFloder") {
jenkinsUrl = String.format("http://url/jenkins/job/%s/config.xml", jobName);
}
if(jobName != null&&type=="buildstatus")
{
jenkinsUrl = "http://url/jenkins";
jenkinsUrl=jenkinsUrl + "/job/" + jobName + "/lastBuild/api/xml";
}
if(jobName != null&&type=="jenkinsjobexist")
{
jenkinsUrl = "http://url/jenkins";
jenkinsUrl=jenkinsUrl + "/job/" +pduName+ "/job/"+ jobName + "/";
}
URI uri = URI.create(jenkinsUrl);
HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
new UsernamePasswordCredentials(jenkinsUser, jenkinsPass));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(host, basicAuth);
CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
// HttpPost httpPost = new HttpPost(uri);
HttpGet request = new HttpGet(uri); // 发送get请求
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
HttpResponse response = httpClient.execute(host, request, localContext);
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
int responseCode = response.getStatusLine().getStatusCode();
// String result = EntityUtils.toString(response.getEntity());
System.out.println(responseCode);
String code=String.valueOf(responseCode);
return code;
}
// HttpClient Post请求(带Xml参数)用于新建jenkinsjob 和新建文件夹
public static Integer httpclientPostWithXml(String jenkinsUser, String jenkinsPass, String pduName,String jobName, String xmlString,String type) throws ClientProtocolException, IOException {
String jenkinsUrl = "";
if (type == "jenkinCreateJob") {
// 登录url
jenkinsUrl = String.format("http://url/jenkins/job/%s/createItem?name=%s", pduName,jobName);
}
if(type=="createFolder") {
jenkinsUrl = String.format("http://url/jenkins/view/all/createItem?name=%s", jobName);
//jenkinsUrl = String.format("http://url/jenkins/view/all/createItem?name=%s", jobName);
}
if(type=="updateJenkinsJob")
{
jenkinsUrl = String.format("http://url/jenkins/job/%s/job/%s/config.xml", pduName,jobName);
}
URI uri = URI.create(jenkinsUrl);
HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
new UsernamePasswordCredentials(jenkinsUser, jenkinsPass));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(host, basicAuth);
CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
HttpPost httpPost = new HttpPost(uri);
// add xmlstring
// List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
// parameters.add(new BasicNameValuePair("xml", xmlStringNew));
// httpPost.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
StringEntity entity = new StringEntity(xmlString);
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "text/xml;charset=ISO-8859-1");
// add xmlstring end
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
HttpResponse response = httpClient.execute(host, httpPost, localContext);
int responseCode = response.getStatusLine().getStatusCode();
// String result = EntityUtils.toString(response.getEntity());
System.out.println(responseCode);
return responseCode;
}
// HttpClient Post请求 用于build jenkinsjob
public static String httpclientPost(String jenkinsUser, String jenkinsPass, String pduName,String jobName,
String buildJobParams,String type) throws ClientProtocolException, IOException {
String jenkinsBuildUrl = "";
if (type=="jenkinsBuildJob"&&buildJobParams != null) {
jenkinsBuildUrl= String.format("http://url/jenkins/job/%s/job/%s/buildWithParameters? %s",pduName,jobName,buildJobParams);
}
if(type=="jenkinsBuildJob") {
jenkinsBuildUrl= String.format("http://url/jenkins/job/%s/job/%s/build", pduName,jobName);}
if(type=="delFolder") {
jenkinsBuildUrl=String.format("http://url/jenkins/view/all/job/%s/doDelete", pduName);
}
URI uri = URI.create(jenkinsBuildUrl);
HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
new UsernamePasswordCredentials(jenkinsUser, jenkinsPass));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(host, basicAuth);
CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
HttpPost httpPost = new HttpPost(uri);
// Add AuthCache to the execution context
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
HttpResponse response = httpClient.execute(host, httpPost, localContext);
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
int responseCode = response.getStatusLine().getStatusCode();
// String result = EntityUtils.toString(response.getEntity());
System.out.println(responseCode);
String code=String.valueOf(responseCode);
return code;
}