zoukankan      html  css  js  c++  java
  • 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"));
        }
    }

    调用:

    成功获取网页内容

  • 相关阅读:
    在Selenium自动化中查找损坏的链接
    WebDriverManager
    在TestNG中重试失败的测试
    算法:汉诺塔
    Java 多线程
    spring ioc原理(看完后大家可以自己写一个spring)
    Apache与Nginx的区别
    Apache与Nginx的区分比较
    Java 设计模式
    Java 理论与实践: 正确使用 Volatile 变量
  • 原文地址:https://www.cnblogs.com/weibanggang/p/11331524.html
Copyright © 2011-2022 走看看