// HttpClient Post请求(带form-data)用于新建pipeline
@SuppressWarnings("unchecked")
public static Integer httpclientPostWithFormdata(String jenkinsUser, String jenkinsPass, String pduName,String pipelineName, String type) throws ClientProtocolException, IOException {
String jenkinsUrl = "";
if (type == "createPipeline") {
//在主页面新建
//jenkinsUrl = String.format("http://jenkins.shishike.com/jenkins/createView");
//在项目里面新建
jenkinsUrl = String.format("http://jenkins.shishike.com/jenkins/job/%s/createView",pduName);
}
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);
String str=String.format("{\"name\": \"%s\", \"mode\": \"au.com.centrumsystems.hudson.plugin.buildpipeline.BuildPipelineView\", \"Submit\": \"OK\"}", pipelineName);
// String str="{\"name\": \"pipelineOne\", \"mode\": \"au.com.centrumsystems.hudson.plugin.buildpipeline.BuildPipelineView\", \"Submit\": \"OK\"}";
//JSONObject jsonObject = new JSONObject();
//jsonObject.put("name", "pipeline22");
//jsonObject.put("mode", "au.com.centrumsystems.hudson.plugin.buildpipeline.BuildPipelineView");
//jsonObject.put("json", str);
//jsonObject.put("Submit", "OK");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("name", pipelineName));
nvps.add(new BasicNameValuePair("mode", "au.com.centrumsystems.hudson.plugin.buildpipeline.BuildPipelineView"));
nvps.add(new BasicNameValuePair("json", str));
nvps.add(new BasicNameValuePair("Submit", "OK"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
//httpPost.setEntity(new StringEntity(jsonObject.toString()));
//httpPost.setHeader("Content-Type", "text/html;charset=UTF-8");
// 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请求(带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://172.16.0.18:18080/jenkins/job/Oldproject/createItem?name=%s", jobName);
jenkinsUrl = String.format("http://jenkins.shishike.com/jenkins/job/%s/createItem?name=%s", pduName,jobName);
}
if(type=="createFolder") {
jenkinsUrl = String.format("http://jenkins.shishike.com/jenkins/view/all/createItem?name=%s", jobName);
}
// if(type=="createPipeline") {
// jenkinsUrl=String.format("http://jenkins.shishike.com/jenkins/viem/all/createItem?name=%s", "pipeline191");
//}
if(type=="updatePipeline") {
jenkinsUrl=String.format("http://jenkins.shishike.com/jenkins/view/%s/config.xml", jobName);
}
if(type=="updateJenkinsJob")
{
//http://jenkins.shishike.com/jenkins/job/Oldproject/job/Oldproject-susiestest2/config.xml
jenkinsUrl = String.format("http://jenkins.shishike.com/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;
}