I'm trying to edit job description in Jenkins. Previously I did it through bash doing 'curl --data-urlencode 'description=ABCDEFGH' --data-urlencode "Submit=Submit" " https://Login/APItoken@jenkinsURL/job/JobName/Build/submitDescription ". Now, I would like to do the same but through Java. This is what I have so far. As always, "it should work but it doesn't" in my opinion:
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);
}
}
Ok, let me hear your thoughts. What I get is the HTML of a site with:
function showTranslationDialog() {
if(!translation.launchDialog)
loadScript("address/plugin/translation/dialog.js");
else
translation.launchDialog();
return false;
}</script></body></html>
if my URL is ".../Build" or
server returned HTTP response code: 403
when my URL's ".../Build/configSubmit" as above in the code.
Thanks in advance!
I would suggest to use jenkins client sdk's to get interacted.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.