passing attributes in apache http client

353 Views Asked by At

I have a curl like as shown below

curl -s -g https://example.com:8086 --tlsv1 --cacert /etc/myfolder/certificate.pem --cert /etc/myfolder/ssl/certificate.pem --key /etc/myfolder/com/certificate.pem

I have used apache java http client for accessing the above curl

 String url = "https://example.com:8086";
 HttpClient client = HttpClientBuilder.create().build();
 HttpGet httpGet = new HttpGet(url);
 httpGet.setHeader("Content-Type", "application/json");
 HttpResponse httpresponse = client.execute(httpGet);
 EntityUtils.toString(httpresponse.getEntity());

but I don't know how to pass certiifcates and attributes such as -s -g --tlsv1 --cacert /etc/myfolder/certificate.pem --cert /etc/myfolder/ssl/certificate.pem --key /etc/myfolder/com/certificate.pem

Can anyone please help me on this

1

There are 1 best solutions below

0
On
SSLContext sslContext = SSLContexts.custom()
        .loadTrustMaterial(new File("/etc/myfolder/certificate.keystore"))
        .loadTrustMaterial(new File("/etc/myfolder/ssl/certificate.keystore"), "store password".toCharArray(), "key password".toCharArray())
        .build();
CloseableHttpClient client = HttpClientBuilder.create()
    .setSSLSocketFactory(new SSLConnectionSocketFactory(
            sslContext,
            new String[] {"TLSv1"},
            null,
            SSLConnectionSocketFactory.getDefaultHostnameVerifier()))
HttpGet httpGet = new HttpGet("https://targethost/");
try (CloseableHttpResponse response1 = client.execute(httpGet)) {
    System.out.println(response1.getStatusLine());
    EntityUtils.consume(response1.getEntity());
}

Please note depending on the format of your PEM files you will likely have to load them into a Java keystore first.