使用 HttpURLConnection 請求 HTTPS 介面,拋出 SSLHandshakeException: PKIX path building failed。經排查,根因為 **JRE 1.6 不支援 SNI (Server Name Indication)**,導致 CDN 虛擬主機返回錯誤憑證。
public String sendGet(String url) {
URL realUrl = new URL(url);
URLConnection connection = realUrl.openConnection();
// ... 設定請求屬性 ...
connection.connect();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "utf-8"));
// ... 讀取回應 ...
}
錯誤堆疊:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target
keytool 匯入 CA 憑證 —— 無效,因憑證本身合法(GoDaddy 簽發)。**虛擬主機機制**:HTTP 靠 Host 標頭區分站點;HTTPS 握手階段尚未傳送 HTTP 標頭,需在 ClientHello 中攜帶域名 —— 即 SNI 擴展。
升級 JRE 至 1.8 (或至少 1.7),重啟 Tomcat,問題即刻解決。
java -version
# 確認版本為 1.8.x
# 重新測試 HTTPS 請求,握手成功,返回正確內容