总结(一)


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值