问题
发送post请求报错
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
原因
SSL证书的问题
解决办法
绕过SSL证书认证即可。
1、HttpClient绕过方法
public HttpClient skipHttpsCertificate() { try { SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } }; ctx.init(null, new TrustManager[] { tm }, null); SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory( ctx, NoopHostnameVerifier.INSTANCE); CloseableHttpClient httpclient = HttpClients.custom() .setSSLSocketFactory(ssf).build(); return httpclient; } catch (Exception e) { return HttpClients.createDefault(); } }
调用方法:
CloseableHttpClient client = (CloseableHttpClient) HikvisionUtil.skipHttpsCertificate(); // 调用生成Client之后其余操作和 HttpClient 请求处理一致 CloseableHttpResponse resp = client.execute(httpPost);
其余的请求方式可自行百度
标签:Java,发送,报错,X509Certificate,new,return,public,HttpClient From: https://www.cnblogs.com/lucky-jun/p/16832528.html