javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

描述

使用 HttpURLConnection 連線時出現異常

原因

所連線的是 https 網站,必須有 SSL 憑證。

解決

將憑證添加至 Java

一、下載網站憑證

二、匯入憑證

keytool -import -alias ALIAS -keystore KEYSTORE -file FILE

ALIAS:此憑證的別名
KEYSTORE:jdk\lib\security\cacerts
FILE:憑證檔案路徑

三、輸入密碼 Enter keystore password:

預設為 changeit

四、確認訊息 Trust this certificate?

yes