Java爬虫https网页内容报错SSLHandshakeException信任(忽略)所有SSL证书

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

原因:https出现信任弹出(访问网页时候弹出是否信任)

解决方案:忽略ssl证书

创建一个类忽略ssl证书

TrustSSL.java
import java.io.*;
import java.net.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.*;

public class TrustSSL {

    private static class TrustAnyTrustManager implements X509TrustManager {

        public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[] {};
        }
    }

    private static class TrustAnyHostnameVerifier implements HostnameVerifier {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }
    public static InputStream HttpsSSL(URL strUrl){

        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },new java.security.SecureRandom());
            HttpsURLConnection conn = (HttpsURLConnection) strUrl.openConnection();
            conn.setSSLSocketFactory(sc.getSocketFactory());
            conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
            //设置超时间为5秒
            conn.setConnectTimeout(5 * 1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            conn.connect();
            //获取服务器响应代码
            int responsecode = conn.getResponseCode();
            if (responsecode == 200) {
                //得到输入流
                return conn.getInputStream();
            } else {
                System.out.println("获取不到 " + strUrl + " 源码,服务器响应代码为:" + responsecode);
                return null;
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }
    public static void main(String[] args) throws Exception {
        HttpsSSL(new URL("url"));
    }
}

调用:

 

成功获取网页内容

 

posted @ 2019-08-10 14:37  韦邦杠  阅读(1875)  评论(0编辑  收藏  举报