详细错误信息:
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) {
- }
- }
来源: http://www.bubuko.com/infodetail-3778972.html