通过HTTP POST文件上传+参数以触发Hudson构建

时间:2010-12-09 12:53:22

标签: java http hudson httpclient hudson-api

目前我正在寻找一种工作方法来上传{+ 3}}以下的文件+字段。目前的问题是Hudson总是抱怨应该提交的表格..(见本文稍后的例外)。但基于我读过的文档,它应该像下面的Java代码片段一样工作......

HttpPost httppost = new HttpPost(triggerJobUrl);
FileBody fileBody = new FileBody(releaseProperties);
StringBody stringBody = new StringBody(svnURL.toString());
MultipartEntity mentity = new MultipartEntity();
mentity.addPart("trunk/release.properties", fileBody);
mentity.addPart("SVNURL", stringBody);
httppost.setEntity(mentity);
HttpResponse response = null;
try {
    response = httpClient.execute(httppost);
} catch (ClientProtocolException e) {
    throw new HudsonException("http protocol error.", e);
} catch (IOException e) {
    throw new HudsonException("connection aborted.", e);
}
if (response.getStatusLine().getStatusCode() != 200) {
    throw new HudsonException("Unexpected status code received " + response.getStatusLine().getStatusCode());
}
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
    try {
        resEntity.consumeContent();
    } catch (IOException e) {
        throw new HudsonException(
                "if an I/O error occurs. This indicates that connection keep-alive is not possible.", e);
    }
}

我目前的Maven依赖关系如下:

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.0.3</version>
</dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpcore</artifactId>
  <version>4.0.1</version>
  <type>jar</type>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpmime</artifactId>
  <version>4.0.3</version>
  <type>jar</type>
  <scope>compile</scope>

例外是:

java.lang.Error: This page expects a form submission
   at org.kohsuke.stapler.RequestImpl.getSubmittedForm(RequestImpl.java:769)
   at hudson.model.ParametersDefinitionProperty._doBuild(ParametersDefinit

1 个答案:

答案 0 :(得分:21)

你可能正在使用url like job / blabla / build你必须尝试使用​​job / blabla / buildWithParameters