When using HttpPost to call the https interface, report an error: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to the requested target
,
Here is a solution below:
/**
* HttpClient error: ”SSLPeerUnverifiedException: peer not authenticated”
* You don't need to import SSL
*
* @param base
* @return
*/
public static HttpClient wrapClient(HttpClient base) {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
throws java.security.cert.CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
throws java.security.cert.CertificateException {
// TODO Auto-generated method stub
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
};
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 ex) {
ex.printStackTrace();
return HttpClients.createDefault();
}
}
Calling wrapclient method to generate httpclient can avoid problems.
@Inject(target = "/infoResourcesManageRest/custom/getAllDataNum", type = InjectTypeExt.CUSTOM_URL)
public synchronized WSResult getAllDataNum(JSONObject json) throws Exception {
Integer talentNum = 0;
Integer companyNum = 0;
Integer organizationNum = 0;
CloseableHttpClient httpClient = HttpClients.createDefault();
httpClient = (CloseableHttpClient) wrapClient(httpClient);
CloseableHttpResponse response = null;
/**
* 1. Little data
* **/
String[] zx = new String[]{"qyjbxx", "dwjbxx"};
for (String s : zx) {
JSONObject jsonObject1 = JSONObject.fromObject("{\n" +
" \"size\": 1,\n" +
" \"page\": 1,\n" +
" \"params\": {\n" +
" \"englishName\": \"" + s + "\"\n" +
" },\n" +
" \"filter\": {}\n" +
"}");
String jsonString = JSON.toJSONString(jsonObject1);
HttpPost httpPost = new HttpPost("https://www.baidu.com");
StringEntity entity = new StringEntity(jsonString, "UTF-8");
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
response = httpClient.execute(httpPost);
JSONObject jsonObject = JSONObject.fromObject(EntityUtils.toString(response.getEntity(), "utf-8"));
JSONObject result = (JSONObject) jsonObject.get("result");
if (result != null) {
Integer totalElements = (Integer) result.get("totalElements");
if (s.equals("qyjbxx")) {
companyNum = companyNum + totalElements;
} else if (s.equals("dwjbxx")) {
organizationNum = organizationNum + totalElements;
}
}
}
}
Read More:
- [Solved] JAVA HttpClient Send Https request certificate error:PKIX path building failed:
- [Solved] maven Import Error: PKIX path building failed
- Maven (http://repo1.maven.org/maven2/): Failed to transfer file and PKIX path building failed: sun.secu
- Initializingbean Interface & Applicationcontextaware Interface in Springboot
- [Solved] cannot call sendError() after the response has been committed
- [Solved] port (127.0.0.1:64444): java.net.SocketException “Interrupted function call: accept failed“
- How to Solve Error: call_and_retry_last allocation failed – javascript heap out of memory
- [Redirect anomaly] Cannot call sendRedirect() after the response has been committed
- [Solved] Tomcat configurate HTTPS error: java.net.SocketException: Permission denied
- Abstract method and static method of java interface
- Cannot call sendRedirect() after the response has been committed
- [Solved] org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession…
- [Solved] java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
- Mybatis reports an error (error building sqlsession.) when using annotations without deleting redundant files
- try-with-resource automatically closes the resources of the AutoClosable interface
- [Solved] Error launching IDEA: Failed to create JVM. JVM Path…
- [Solved] seata:Error creating bean with name ‘globalTransactionScanner‘ defined in class path resource
- [Solved] IntelliJ IDEA Start Error: Failed to create JVM.JVM Path
- [Solved] Circular view path [index]: would dispatch back to the current handler URL [] again. Che
- [Solved] Java.lang.BootstrapMethodError: call site initialization exception