Code example for HttpsURLConnection

Methods: getURLsetHostnameVerifiersetSSLSocketFactory

0
    // This method has been added in support of https://bugzilla.redhat.com/show_bug.cgi?id=690430. 
    private static void disableCertificateVerification(HttpsURLConnection connection) {
        try { 
            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, new TrustManager[]{NO_OP_TRUST_MANAGER}, new java.security.SecureRandom());
            connection.setSSLSocketFactory(sslContext.getSocketFactory());
            connection.setHostnameVerifier(NO_OP_HOSTNAME_VERIFIER);
        } catch (Exception e) {
            logWarnWithStackTraceOnlyIfDebugEnabled("Failed to disable certificate and hostname validation on URLConnection for [" 
                    + connection.getURL() + "].", e);
        } 
    } 
 
    private static void logWarnWithStackTraceOnlyIfDebugEnabled(String message, Exception e) {
        if (LOG.isDebugEnabled()) {
            LOG.warn(message, e);
        } else { 
            LOG.warn(message + " (enable DEBUG logging to see stack trace): " + e);
        } 
    } 
Contextual code suggestions in your IDE  Get Codota for Java