我试图通过java代码触发jenkins的工作。我在网上找到了源代码,我没能让它正常工作。我一直得到“服务器返回的HTTP错误代码403"
请查找示例代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.Base64;import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class JenkinsScraper {
public static void main(String[] args) throws ClientProtocolException, IOException{
try {
URL url = new URL ("http://localhost:8080/job/SampleJob/build");
String encoding = Base64.getEncoder().encodeToString(("admin:admin").getBytes());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty ("Authorization", "Basic " + encoding);
InputStream content = (InputStream)connection.getInputStream();
BufferedReader in = new BufferedReader (new InputStreamReader (content));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch(Exception e) {
e.printStackTrace();
}
}我甚至试图跟随这个URL通过命令行触发Jenkins的工作,但我只是一个初学者,我不明白。
我是不是漏掉了什么。任何关于这方面的建议都是有帮助的。任何有用的例子都会很好。提前谢谢。
发布于 2018-10-18 17:50:34
您必须传递下面列出的属性,并在Jenkins配置中更改配置。在“触发器远程构建”选项的Jenkins配置中设置带有某些值的令牌,在Jenkins URL的查询参数中使用该值
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
connection.setRequestProperty("Accept-Language", "en-US");
connection.setRequestProperty("Accept-Encoding", "gzip, deflate");

发布于 2020-07-21 13:02:38
Following code working as expected:
try {
URL url = new URL("http://localhost:8080/job/TestPrj/build"); // Jenkins URL localhost:8080, job named 'test'
String user = "username"; // username
String pass = "jenkinsapitoken"; // password or API token
String authStr = user + ":" + pass;
String encoding = Base64.getEncoder().encodeToString(authStr.getBytes("utf-8"));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + encoding);
InputStream content = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}https://stackoverflow.com/questions/50154246
复制相似问题