我正在尝试编辑Jenkins的职位描述。以前我通过bash做'curl --data-urlencode'描述= ABCDEFGH' - data-urlencode“Submit = Submit”“https://Login/APItoken@jenkinsURL/job/JobName/Build/submitDescription”。现在,我想通过Java做同样的事情。这就是我到目前为止所拥有的。和往常一样,“我认为它应该有用,但它不会”:
import java.io.*;
import java.util.*;
import java.net.URL;
import java.net.URLEncoder;
import java.net.HttpURLConnection;
public class JenkinsDesUpdate {
public static void main(String[] args) throws Exception {
URL url = new URL("https://Login:APItoken@jenkinsURL/job/JobName/Build/configSubmit");
Map <String,Object> params = new LinkedHashMap<>();
params.put("description", "THIS IS A NEW DESCRIPTION");
params.put(DisplayName","THIS IS A NEW DISPLAY NAME");
StringBuilder postData = new StringBuilder();
for (Map.Entry <String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
for (int c; (c = in.read()) >= 0;)
System.out.print((char)c);
}
}
好的,让我听听你的想法。我得到的是一个网站的HTML:
function showTranslationDialog() {
if(!translation.launchDialog)
loadScript("address/plugin/translation/dialog.js");
else
translation.launchDialog();
return false;
}</script></body></html>
如果我的网址是“... / Build”或
服务器返回HTTP响应代码:403
当我的URL在代码中的“... / Build / configSubmit”时如上所示。
提前致谢!