日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

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

發(fā)布時(shí)間:2025/4/16 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java爬虫https网页内容报错SSLHandshakeException信任(忽略)所有SSL证书 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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出現(xiàn)信任彈出(訪問網(wǎng)頁時(shí)候彈出是否信任)

解決方案:忽略ssl證書

創(chuàng)建一個(gè)類忽略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());//設(shè)置超時(shí)間為5秒conn.setConnectTimeout(5 * 1000);//防止屏蔽程序抓取而返回403錯(cuò)誤conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");conn.connect();//獲取服務(wù)器響應(yīng)代碼int responsecode = conn.getResponseCode();if (responsecode == 200) {//得到輸入流return conn.getInputStream();} else {System.out.println("獲取不到 " + strUrl + " 源碼,服務(wù)器響應(yīng)代碼為:" + 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"));} }

調(diào)用:

?

成功獲取網(wǎng)頁內(nèi)容

?

轉(zhuǎn)載于:https://www.cnblogs.com/weibanggang/p/11331524.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的Java爬虫https网页内容报错SSLHandshakeException信任(忽略)所有SSL证书的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。