[ 登录 ] - [ 注册 ] | 免费代理IP 数据无忧_无忧代理IP_DATA5U最新上线 | 代码示例DEMO | IP测试视频 | 用户协议

JAVA 请求HTTPS网址提示握手或者SSL证书错误javax.net.ss1.ssLHandshakeException: sun.security.validator.ValidatorException: PKIX path building

作者:数据无忧   时间:2024-06-21 20:15:26
无忧代理IP推广返现活动 使用JAVA请求https网址报错javax.net.ss1.ssLHandshakeException: sun.security.validator.ValidatorException: PKIX path building...

是因为证书错误导致的,可以选择忽略证书,或者信任所有证书即可轻松解决。

下面是信任所有证书的解决方案。

1. 新建一个类TrustCertsUtil.java

public class TrustCertsUtil {
	
	public static void trustAllHttpsCertificates() {

		// 如果爬虫请求HTTPS网址,必须加入这两行
		System.setProperty("jdk.http.auth.proxying.disabledSchemes", "");
		System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
		
		try {
	        javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
	        javax.net.ssl.TrustManager tm = new MITM();
	        trustAllCerts[0] = tm;
	        javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("TLS");
	        sc.init(null, trustAllCerts, null);
	        javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
		} catch (Exception e) {
			System.err.println("【DATA5U】设置证书出错,原因:" + e.getMessage());
		}
	}
	
    static class MITM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public boolean isServerTrusted(java.security.cert.X509Certificate[] certs) {
            return true;
        }

        public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) {
            return true;
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
                throws java.security.cert.CertificateException {
            return;
        }

        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
                throws java.security.cert.CertificateException {
            return;
        }
    }
	
}

2. 在程序发送请求之前调用TrustCertsUtil.trustAllHttpsCertificates();

	protected static void crawl(String targetUrl, String httpsIpport) {
		try {
			
			TrustCertsUtil.trustAllHttpsCertificates();
			
			long startTime = System.currentTimeMillis();
			
	    	HttpURLConnection connection = null;
	    	URL link = new URL(targetUrl);
	    	
			Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress((httpsIpport.split(":"))[0], Integer.parseInt((httpsIpport.split(":"))[1])));
    		connection = (HttpURLConnection)link.openConnection(proxy);
	    	
	    	connection.setDoOutput(true);
	    	connection.setRequestProperty("Accept", "*/*");
	    	connection.setRequestProperty("Upgrade-Insecure-Requests", "1");
	    	connection.setUseCaches(false);
	    	connection.setConnectTimeout(6000);
	    	
	        String line = null;
	        StringBuilder html = new StringBuilder();
	        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
	        while((line = reader.readLine()) != null){
	        	html.append(line);
	        }
	        try {
				if (reader != null) {
					reader.close();
				}
			} catch (Exception e) {
			}
	        
	        connection.disconnect();
	       
	        long endTime = System.currentTimeMillis();
	        
			System.out.println("[OK]" + "→→→→→" + proxy.type() + "  " + targetUrl + "  " + (endTime - startTime) + "ms  " + connection.getResponseCode() + "   " + html.toString());
		} catch (Exception e) {
			System.err.println("[ERR]" + "→→→→→" + targetUrl + "  " + e.getMessage());
		}
	}
	



福利:现在通过开发者购买代理IP,购买后联系客服最高可获   返现20%   到你的支付宝(最低35元,最高1440元)
无忧代理IP(www.data5u.com)原创文章,转载请注明出处。

微信公众号
关注微信公众号