调用通过HttpPost调用Hudson rest api不起作用

时间:2013-09-08 14:15:59

标签: java rest post hudson

我想调用Hudson Rest api来使用HttpPost请求复制作业。以下是我写的代码:

public void copy(String hudsonBaseURL, String originJobName, String newJobName) throws Exception {
    HttpPost post = new HttpPost(hudsonBaseURL + "/createItem");

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("name", newJobName));
    urlParameters.add(new BasicNameValuePair("mode", "copy"));
    urlParameters.add(new BasicNameValuePair("from", originJobName));

    post.setHeader("Content-type", "text/plain");
    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    System.out.println(post.getEntity().getContent());
    try {
      HttpResponse response = httpClient.execute(post);
      System.out.println("Response Code : " + response.getStatusLine().getStatusCode());


    } finally {
      post.releaseConnection();
    }
  }

但是上面的代码返回404错误代码并说它无法找到参数。

我使用了这个链接:http://blog.zenika.com/index.php?post/2009/02/22/Remote-access-API-et-Hudson来编写我的代码,但唯一不同的是,我使用的是HttpPost而不是Post,这是一个旧的api。

这里有什么想法吗?

编辑:

阻止内容类型的行,它开始工作,但不知道为什么

post.setHeader("Content-type", "text/plain");

1 个答案:

答案 0 :(得分:0)

您的服务器需要的响应不是text/plain类型,例如 - application/jsonContent-type标头描述了服务器期望获得的数据类型。添加此标头后,强制您的请求为服务器无法处理的text/plain类型。当您删除它时,我猜(无法确定无法查看您的日志)请求是以正确的格式发送的。