Jsoup抓取Https出现unable to find valid certification path to requested target
  0ZwfR6X6Muxs 2023年11月18日 36 0

解决办法如下:

方法一:

  1. 增加前置初始化操作
static public void init() {
        try {
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, new X509TrustManager[]{new 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[0];
                }
            }}, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  1. 在进行网站抓取
public static void main(String[] args) throws Exception {
        String url = "https://info.hebei.gov.cn/hbszfxxgk/6898876/6898925/6899014/6906934/748af770/index1.html";
        init();
        String UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36";
        Document countDocument = Jsoup.connect(url).timeout(30000).userAgent(UA).get();
        System.out.println(countDocument);
    }

方法二:使用httpUtil请求抓取网页结果

//首先使用工具类获取一下网页结构
 String strHtml = HttpUtil.get(url, 30000);
 //在用JSOUP解析HTML结构
 Document countDocument = Jsoup.parse(strHtml);
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月18日 0

暂无评论

推荐阅读
  wURKzOHw9Irf   2023年12月24日   29   0   0 HTMLicoicohtml
  8l4CZpTOKa7P   2023年12月26日   40   0   0 htmlhtml
  dwHry2iKGG0I   2023年12月26日   31   0   0 githubgithubhtmlhtml
0ZwfR6X6Muxs