Q:

HttpClient java.net.UnknownHostException exception when the CURL command passes

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

A:

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,

