반응형
public static String httpsGet(String strURL) throws Exception
{
    URL url = null;
    HttpsURLConnection con = null;
    String ret = new String();

    try {
        url = new URL(strURL);
        ignoreSsl();
        con = (HttpsURLConnection)url.openConnection();


        BufferedReader br = null;
        br = new BufferedReader(new InputStreamReader(con.getInputStream()));

        String input = null;

        while ((input = br.readLine()) != null){
            ret += input;
        }

        br.close();
    }
    catch (IOException e) {
        ExceptionUtil.getStackTrace(e);
    } finally {
        if (con != null) {
            con.disconnect();
        }
    }

    return ret;

}

 

 

public static void ignoreSsl() throws Exception{
    HostnameVerifier hv = new HostnameVerifier() {
    public boolean verify(String urlHostName, SSLSession session) 
            return true;
        }
    };
    trustAllHttpsCertificates();
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
}

private static void trustAllHttpsCertificates() throws Exception {
    TrustManager[] trustAllCerts = new TrustManager[1];
    TrustManager tm = new miTM();
    trustAllCerts[0] = tm;
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, null);
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}

static class miTM implements TrustManager,X509TrustManager {
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }

    public boolean isServerTrusted(X509Certificate[] certs) {
        return true;
    }

    public boolean isClientTrusted(X509Certificate[] certs) {
        return true;
    }

    public void checkServerTrusted(X509Certificate[] certs, String authType)
            throws CertificateException {
        return;
    }

    public void checkClientTrusted(X509Certificate[] certs, String authType)
            throws CertificateException {
        return;
    }
}

 

 

출처 : https://ram2ram2.tistory.com/16

반응형