sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target (找不到要求目標的有效憑證路徑)

嘗試使用 HttpURLConnection 發出 GET Request
報了這個異常
經過測試
如果請求路徑為 http 是沒問題的
只有 https 不行
因此的確是 SSL 憑證問題

解決

到目標網站下載憑證

將憑證匯入 JDK 或 JRE

cd jdk
bin\keytool -keystore lib\security\cacerts -storepass changeit -import -file "C:\Downloads\twse.cer" -alias twse
  • -keystore:jdk 的憑證路徑為 lib\security\cacerts
  • -storepass:密碼預設是 changeit,除非有改過
  • -import:匯入
  • -file:剛剛下載的憑證檔位置
  • -alias:別名

信任此憑證?
輸入 yes