2012-05-17 57 views
2

我無法從我的網站(不是其他網站)獲得證書。我嘗試了一些HttpsURLConnection和方法​​的解決方案,但沒有解決問題。無法從網站獲取服務器證書

URL httpsURL = new URL("https://www.google.com.br/"); 
HttpsURLConnection connection = (HttpsURLConnection)httpsURL.openConnection(); 
Certificate[] certs = connection.getServerCertificates(); 

我收到一個異常說「getServerCertificates無法解析」。

我不認爲它有必要使用密鑰庫,是嗎?

+1

這是一個好主意,總是讓它從你的問題,你所使用的語言清晰。儘管有經驗的Java程序員可能會立即將其識別爲Java代碼,但其他人(比如我)很難知道您正在使用哪種語言。 – rlandster

+0

哦對不起,我會在下一個問題中記住。謝謝 –

回答

1

我得到一個異常說getServerCertificates解決不了..

真奇怪。你確定你在使用正確的類嗎?如果我運行此代碼:

import java.io.IOException; 
import java.net.URL; 
import java.security.cert.Certificate; 

import javax.net.ssl.HttpsURLConnection; 

public class Test { 
    public static void main(String[] args) throws IOException { 
     URL httpsURL = new URL("https://www.google.com.br/"); 
     HttpsURLConnection connection = (HttpsURLConnection) httpsURL.openConnection(); 
     connection.connect(); 
     Certificate[] certs = connection.getServerCertificates(); 
     for (Certificate cer : certs) { 
      System.out.println(cer.getPublicKey()); 
     } 
    } 
} 

我得到這樣一個結果:

Sun RSA public key, 1024 bits 
    modulus: 13069990984429476578... 
    public exponent: 65537 
Sun RSA public key, 1024 bits 
    modulus: 14179907349200548596... 
    public exponent: 65537 

驗證SSL什麼插座使用的是工廠,也許有什麼不妥。此添加到您的代碼,看看它從什麼樣的結果(作爲一個例子,對我來說是com.sun.net.ssl.internal.ssl.SSLSocketFactoryImpl):

System.out.println(connection.getSSLSocketFactory().getClass()); 
System.out.println(connection.getDefaultSSLSocketFactory().getClass()); 
+0

現在它工作正常! 謝謝! –

相關問題