1

I am trying to use httpclient to make make a call to Jenkins to get a list of jobs.

When I run my code, I get an UnknownHostException.

I tried to make the same request using curl and I was able to get the result. I am not sure how to interpret this.

void nwe() throws ClientProtocolException, IOException {
    HttpHost target = new HttpHost("https://<JENKINS_URL>/api");
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope(target.getHostName(), target.getPort()),
            new UsernamePasswordCredentials("username", "password"));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    HttpGet httpGet = new HttpGet("/json");
    httpGet.setHeader("Content-type", "application/json");
    BasicScheme basicAuth = new BasicScheme();
    HttpClientContext localContext = HttpClientContext.create();
    CloseableHttpResponse response1 = httpclient.execute(target, httpGet, localContext);
    System.out.println(response1.getStatusLine());

}

The CURL command on the same URL gives me the expected output

Thanks, Amar

1
  • Not much to go on, but use curl -v and check whether you are connecting via a proxy server. Make sure you are using fully-qualified host names. Commented May 19, 2015 at 9:52

1 Answer 1

2

Read the JavaDoc for HttpHost:

Parameters: hostname - the hostname (IP or DNS name)

So you should use just (omit the protocol and context):

HttpHost target = new HttpHost( "<JENKINS_URL>" );

and then HttpGet the /api/json part.

Cheers,

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you SIr!!!! This led to a problem where the host name verification was failing. I took care of it and now it works...I have accepted this as an answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.