Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4105

API Connectivity using "PKCS12" certificate file

$
0
0

I am getting below Errorjavax.net.ssl.SSLHandshakeException: PKIX path building failed:

sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

import org.apache.hc.client5.http.classic.methods.HttpPost;import org.apache.hc.client5.http.classic.methods.CloseableHttpResponse;import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;import org.apache.hc.client5.http.impl.classic.HttpClients;import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;import org.apache.hc.core5.ssl.SSLConnectionSocketFactory;import org.apache.hc.core5.ssl.SSLContexts;import org.apache.hc.core5.http.io.entity.StringEntity;import javax.net.ssl.SSLContext;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.security.KeyStore;public class CurlToJava {    private static final String P12_FILE_PATH = "path/to/your/pfa-data-service.p12";    private static final String P12_PASSWORD = "PassPhrase";    private static final String TARGET_URL            = "http://B17121-"+"vip.nam.nsroot:100/monitoringDaily/api/download" +"/cs/cars/system%20Views/Obliger%20fund%20Details" +"(as69734_202405080737)?soeId=system&delimiter=%7C"+"&addHeadandTail=true&filename=testDownload";    private static final String AUTH_TOKEN = "0cb93babee5f6370007632b93365109a8";    private static SSLContext createSSLContext() throws Exception {        // Load the PKCS12 KeyStore        KeyStore keyStore = KeyStore.getInstance("PKCS12");        try (FileInputStream keyStoreInputStream = new FileInputStream(P12_FILE_PATH)) {            keyStore.load(keyStoreInputStream, P12_PASSWORD.toCharArray());        }        return SSLContexts.custom()                .loadKeyMaterial(keyStore, P12_PASSWORD.toCharArray())                .build();    }    public static void main(String[] args) {        try {            SSLContext sslContext = createSSLContext();            SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);            CloseableHttpClient httpClient = HttpClients.custom()                    .setConnectionManager(                            PoolingHttpClientConnectionManagerBuilder.create()                                    .setSSLSocketFactory(sslSocketFactory)                                    .build()                    )                    .build();            HttpPost httpPost = new HttpPost(TARGET_URL);            // Set your headers            httpPost.setHeader("accept", "*/*");            httpPost.setHeader("AUTH_TOKEN", AUTH_TOKEN);            httpPost.setHeader("Content-type", "application/json");            // Execute HTTP POST request            try (CloseableHttpResponse response = httpClient.execute(httpPost);                 InputStream responseStream = response.getEntity().getContent();                 OutputStream outputStream = new FileOutputStream("testDownload.csv")) {                byte[] buffer = new byte[1024];                int bytesRead;                while ((bytesRead = responseStream.read(buffer)) != -1) {                    outputStream.write(buffer, 0, bytesRead);                }                System.out.println("Response Code: " + response.getCode());                System.out.println("Response downloaded to testDownload.csv");            }        } catch (Exception e) {            e.printStackTrace();        }    }}

with Above code I am getting Errorjavax.net.ssl.SSLHandshakeException: PKIX path building failed:

sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target


Viewing all articles
Browse latest Browse all 4105


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>