Get certificate and add it to a Java truststore, when only having https URL?

前端 未结 3 802
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 22:54

I\'m trying to send push notifications to Android devices through the Google Cloud Message servers.

The URL we use to do that is:

https://android.goo         


        
3条回答
  •  粉色の甜心
    2021-01-05 23:33

    I've been able to save the certificates through the following Java code:

    public void testConnectionTo(String aURL) throws Exception {
            URL destinationURL = new URL(aURL);
            HttpsURLConnection conn = (HttpsURLConnection) destinationURL.openConnection();
            conn.connect();
            Certificate[] certs = conn.getServerCertificates();
            System.out.println("nb = " + certs.length);
            int i = 1;
            for (Certificate cert : certs) {
                System.out.println("");
                System.out.println("");
                System.out.println("");
                System.out.println("################################################################");
                System.out.println("");
                System.out.println("");
                System.out.println("");
                System.out.println("Certificate is: " + cert);
                if(cert instanceof X509Certificate) {
                    try {
                        ( (X509Certificate) cert).checkValidity();
                        System.out.println("Certificate is active for current date");
                        FileOutputStream os = new FileOutputStream("/home/sebastien/Bureau/myCert"+i);
                        i++;
                        os.write(cert.getEncoded());
                    } catch(CertificateExpiredException cee) {
                        System.out.println("Certificate is expired");
                    }
                } else {
                    System.err.println("Unknown certificate type: " + cert);
                }
            }
        }
    

    And import them to the truststore:

    keytool -import -alias GoogleInternetAuthority -file myCert1 -keystore truststore
    

提交回复
热议问题