我正在尝试通过使用HttipClient来创建工作。当我运行下面的代码我正在变得像 响应状态:400Bad请求 我无法创造工作。任何人请帮我代码如下
public class JenkinsHttpClient {
public static void main(String[] args) throws Exception {
String jenkinsUrl = "JENKINS_URL";
String jobName = "testjobonnode2";
String buildToken = "build";
String username = "username";
String password = "password";
createJobRemote(jenkinsUrl, username, password, "apijob1", 20, 30);
}
public static void createJobRemote(String jenkinsUrl, String username,
String password, String jobName, int daysToKeep, int numToKeep)
throws Exception {
StringBuffer JOB_CONFIG_XML_SB = new StringBuffer("here i wrote some xml configurations");
String job_config_xml = JOB_CONFIG_XML_SB.toString();
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials(username, password));
BasicScheme basicAuth = new BasicScheme();
BasicHttpContext context = new BasicHttpContext();
context.setAttribute("preemptive-auth", basicAuth);
String getUrl = jenkinsUrl + "createItem?name=" + jobName;
HttpPost post = new HttpPost(getUrl);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
post.setEntity(new StringEntity(URLEncoder.encode(job_config_xml,
"UTF-8")));
System.out.println("Request:" + post.getRequestLine());
BufferedReader br = new BufferedReader(new InputStreamReader(
(post.getEntity().getContent())));
String b = null;
while ((b = br.readLine()) != null) {
System.out.println(b);
}
try {
HttpResponse response = client.execute(post, context);
System.out.println("\n Response Status:"
+ response.getStatusLine().getStatusCode()
+ response.getStatusLine().getReasonPhrase());
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Job request posted successfully");
}
}
答案 0 :(得分:0)
要创建作业,用户需要设置Content-Type:application / xml标题