总结(二)


// 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;
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值