gpt4 book ai didi

java - 使用 Rest API 更新 Jenkins 作业配置时出错

转载 作者:太空宇宙 更新时间:2023-11-04 11:57:23 25 4
gpt4 key购买 nike

我正在尝试使用我的 java 代码使用 Jenkins 的 Rest API 更改作业的配置,但它无法连接到 Jenkins url。

请建议我进行更改,以便我可以在不重新启动 Jenkins 的情况下更新我的作业。我正在使用 Jenkins 2.19.4。是否有任何 jenkins 设置阻止我连接到 jenkins config.xml 或者 java 代码有问题?

try
{
String fileDir = "D:\\"; // upload directory
String fileName = "config.xml";
URL url = new URL("http://myjenkins/job/test/config.xml"); // Jenkins URL localhost:8080, job named 'test'

String user = "username"; // username
String pass = "password"; // password or API token
String authStr = user +":"+ pass;
String encoding = DatatypeConverter.printBase64Binary(authStr.getBytes("utf-8"));

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty ("Authorization", "Basic " + encoding);

try
{

String filePath = "D:\\config.xml";
FileInputStream inputStream = new FileInputStream(new File(filePath));
FileReader fr = new FileReader(new File(filePath));
BufferedReader br = new BufferedReader(fr);

String sCurrentLine;
System.out.println(sCurrentLine = br.readLine());
OutputStream os = connection.getOutputStream();

BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
while ((sCurrentLine = br.readLine()) != null) {
//System.out.println("not going inside!!!!");
writer.write(sCurrentLine);
System.out.println(sCurrentLine);
}
writer.flush();
writer.close();
os.close();
int responseCode=connection.getResponseCode();
System.out.println(responseCode);
}
catch(Exception e)
{
e.printStackTrace();
}

}
catch(Exception e)
{
e.printStackTrace();
}

最佳答案

403响应的原因是CSRF安全。要么从 Jenkins 禁用该选项。或者为每个http请求设置Crumb。

关于java - 使用 Rest API 更新 Jenkins 作业配置时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41242430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com