总结(一)

13人阅读 评论(0) 收藏 举报
分类:


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


查看评论

匿名发信六法

匿名发信六法吴建蓉 01-4-30 上午 10:36:01匿名发信的方法很多,主要有以下几种:1、通过可以匿名发信的程序发信如匿名信使就是这样一个软件。如右图所示,软件使用很简单,就不多说了。这里要说...
  • tchaikov
  • tchaikov
  • 2001-05-07 17:36:00
  • 1063

Jenkins API

1.背景 Jenkins具有丰富的插件生态,足以满足我们日常工作的需求;如果我们想通过具体的Jenkins任务直接对外提供服务,而我们不想将内部的具体实现对外暴露(否则,需添加对应的用户权限,通过页...
  • hello_worldee
  • hello_worldee
  • 2017-07-26 09:30:27
  • 261

jenkins带参数的api

官方文档在这里https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API也可以使用http://server/job/myjob/bui...
  • li575098618
  • li575098618
  • 2015-08-22 02:24:43
  • 958

Jenkins REST API 使用指北

作为持续交付开源工具中最出名的一个,Jenkins在业界使用范围很广。但笔者了解到绝大多数使用者都在考虑将 Jenkins 作为其持续交付系统的一个组件来使用,而恰好 Jenkins 也提供了强大的 ...
  • xiaosongluo
  • xiaosongluo
  • 2016-10-12 14:43:59
  • 8695

远程调用jenkis操作的一些API

以下的主要的api代码,相应的工程在另外附上 package jenkinsApi; import java.io.File; import java.io.IOException; ...
  • ljj_9
  • ljj_9
  • 2017-04-14 14:14:56
  • 922

持续集成JenkinsAPI常见用法

https://my.oschina.net/sanpeterguo/blog/197931 摘要: jenkins(持续集成开源工具)提供了丰富的api接口,基本上所有的操作都可以使用curl...
  • u010781856
  • u010781856
  • 2016-09-25 00:11:00
  • 3627

jenkins常用API

执行机(节点) Jenkins credentials
  • snow_114
  • snow_114
  • 2017-04-17 20:38:05
  • 2061

Jenkins+postman+newman之API全自动化测试

1 背景本文要介绍的环境在我司已经投入使用,举个简单的真实使用场景,开发提供了300多个API,每个API都有各种参数,所以我们会先在postman中为这300多个API编写300*n个testcas...
  • wanglin_lin
  • wanglin_lin
  • 2016-07-19 21:53:16
  • 18390

总结(一)

public static String httpclientGet(String jenkinsUser, String jenkinsPass, String pduName,String job...
  • intelrain
  • intelrain
  • 2018-04-15 23:08:33
  • 13

jenkins api

jenkins api curl
  • LvShuiLanTian
  • LvShuiLanTian
  • 2017-01-20 11:11:00
  • 371
    个人资料
    持之以恒
    等级:
    访问量: 7748
    积分: 661
    排名: 7万+