인디노트

How To Avoid javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated Problem Using Apache HttpClient 본문

소스 팁/Java, Android, Kotlin

How To Avoid javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated Problem Using Apache HttpClient

인디개발자 2018. 5. 31. 17:48

public static HttpClient wrapClient(HttpClient base) {

    try {

        SSLContext ctx = SSLContext.getInstance("TLS");

        X509TrustManager tm = new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { }


            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { }


            public X509Certificate[] getAcceptedIssuers() {

                return null;

            }

        };

        ctx.init(null, new TrustManager[]{tm}, null);

        SSLSocketFactory ssf = new SSLSocketFactory(ctx);

        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        ClientConnectionManager ccm = base.getConnectionManager();

        SchemeRegistry sr = ccm.getSchemeRegistry();

        sr.register(new Scheme("https", ssf, 443));

        return new DefaultHttpClient(ccm, base.getParams());

    } catch (Exception ex) {

        return null;

    }

}

반응형
Comments