[Solved] HttpPost Call https Interface error: PKIX path building failed

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: