Quantcast
Channel: Resolving javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed Error? - Stack Overflow
Viewing all articles
Browse latest Browse all 40

Answer by Emmajiugo for Resolving javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed Error?

$
0
0

For OkHttpClient, this solution worked for me.It might help someone using the library...

try {            String proxyHost = "proxy_host";            String proxyUsername = "proxy_username";            String proxyPassword = "proxy_password";            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, "port goes here"));            // Create a trust manager that does not validate certificate chains            TrustManager[] trustAllCerts = new TrustManager[]{                new X509TrustManager() {                    @Override                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {                        return new java.security.cert.X509Certificate[]{};                    }                    @Override                    public void checkClientTrusted(                            java.security.cert.X509Certificate[] certs, String authType) {                    }                    @Override                    public void checkServerTrusted(                            java.security.cert.X509Certificate[] certs, String authType) {                    }                }            };            // Install the all-trusting trust manager            final SSLContext sslContext = SSLContext.getInstance("SSL");            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());            // Create an ssl socket factory with our all-trusting manager            final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();            OkHttpClient client = new OkHttpClient().newBuilder()                    .sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0])                    .hostnameVerifier((hostname, session) -> true)                    .connectTimeout(timeout, TimeUnit.SECONDS)                    .proxy(proxy)                    .proxyAuthenticator((route, response) -> {                        String credential = Credentials.basic(proxyUsername, proxyPassword);                        return response.request().newBuilder()                                .header("Proxy-Authorization", credential)                                .build();                    })                    .build();            MediaType mediaType = MediaType.parse("application/json");            RequestBody requestBody = RequestBody.create(payload, mediaType);            Request request = new Request.Builder()                    .url(url)                    .header("Authorization", "authorization data goes here")                    .method(requestMethod, requestBody)                    .build();            Response response = client.newCall(request).execute();            resBody = response.body().string();            int responseCode = response.code();        } catch (Exception ex) {        }

Viewing all articles
Browse latest Browse all 40

Trending Articles



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