1

I am using the following function from https://wiki.jenkins.io/display/JENKINS/Authenticating+scripted+clients.

import java.io.IOException;
import java.net.URI;

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 String scrape(String urlString, String username, String password) throws ClientProtocolException, IOException {
        URI uri = URI.create(urlString);
        HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username, password));
        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(host, basicAuth);
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
        HttpGet httpGet = new HttpGet(uri);
        // Add AuthCache to the execution context
        HttpClientContext localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);

        HttpResponse response = httpClient.execute(host, httpGet, localContext);

        return EntityUtils.toString(response.getEntity());
    }

}

In the main function I am calling scrape, I pass the job URL as urlString, username as username and API key as password.

JenkinsScraper jenkinsScraper = new JenkinsScraper();
System.out.println(jenkinsScraper.scrape("{jenkins_server}/job/{jobname}/api/json", "username", "APIkey"));

I receive a response which contains an error page that says your browser does not support JavaScript.

<!-- template name: form.autopost.template.html -->


<html>
    <head>
        <title>Submit Form</title>
        <meta name="referrer" content="origin"/>
        <meta http-equiv="x-ua-compatible" content="IE=edge" />
    </head>
    <body onload="javascript:document.forms[0].submit()">
       <noscript>
            <p>
                <strong>Note:</strong> Since your browser does not support JavaScript, you must press the Resume button once to proceed.
            </p>
        </noscript>
        <form method="post" action="/SSO.saml2">
                        <input type="hidden" name="RelayState" value="5mzwkrmdQ3OWkZFOylv0DZPFuBWaak"/>
                        <noscript><input type="submit" value="Resume"/></noscript>
        </form>
    </body>
</html>

How can I extract the build results from Jenkins page?

  • 1
    Try this : https://stackoverflow.com/questions/30078081/unable-to-find-valid-certification-path-to-requested-target-java – Eytan Avisror Jan 22 '18 at 12:11
  • Thanks for your response. I tried this and got rid of the error. But the response I received from the Jenkins server does not contain the desired result. I would like to point out that the Jenkins server uses SSO authentication and thus I can get the desired results upon calling {jenkins_server}/job/{jobname}/api/json from the browser. – nishantbansal2509 Jan 22 '18 at 12:35

0 Answers0