詳細錯誤信息:
SunCertPathBuilderException: unable to find valid certification path to requested target
問題原因:
爬相關數據,因該網站有SSL加密,故無法爬取。
問題解決之核心代碼:
/** * 繞過HTTPS驗證 */ static public void initTSL() { try { SSLContext context = SSLContext.getInstance("TLS"); context.init(null, new X509TrustManager[]{new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }}, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory()); } catch (NoSuchAlgorithmException e) { } catch (KeyManagementException e) { } }