在Java中将curl请求复制到Jenkins

时间:2019-01-22 08:14:53

标签: java jenkins curl jenkins-api

有100个这样的问题,但我找不到我的问题的有效答案。

使用curl我有一个正在工作的请求,响应为201: created

curl -i -X POST https://username:APITOKEN@jenkins-server/job/jobName/buildWithParameters?token=TOKEN -H "Jenkins-Crumb:xxxx"

但是,如果我尝试用Java复制相同的请求,则会得到403 No valid crumb was included in the request。 CSRF已激活(启用了代理兼容性)。

我的Java实现(使用org.apache.httpcomponents:httpcore v.4.4.9):

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;

HttpClient client = HttpClients.createDefault();
String url = "https://username:" + apiToken + "@" + "jenkins-server/job/jobName/buildWithParameters?token=TOKEN";
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Jenkins-Crumb", "xxxx");
HttpResponse response = client.execute(httpPost);

我想念什么?

编辑 我也尝试过使用其他Java库(Unirest,OkHttp),得到相同的响应代码403还尝试了Python(请求库),它的工作原理像一个超级魅力:

requests.request("POST", "https://username:ApiToken@jenkins-server/job/jobname/buildWithParameters", data="", headers={"Jenkins-Crumb": "xxx"}, params={"token":"TOKEN"})

0 个答案:

没有答案