sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target


httpclient-4.5.jar 定時發送http包,忽然有一天報錯,http證書變更引起的。

之前的代碼

try {

            CloseableHttpClient httpClient = buildDefaultHttpClient();
            String url = domain.getUrl();
            HttpGet httpGet = new HttpGet(url);
            httpGet.addHeader("User-Agent", NetUtil.INSPECTOR_USER_AGENT);
            httpGet.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            httpGet.addHeader("Accept-Encoding", "gzip, deflate");
            httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.9");

            CloseableHttpResponse resp = httpClient.execute(httpGet);

            String responseBody = EntityUtils.toString(resp.getEntity(), "utf-8");

            respEnd = DateUtil.toEpochMilliseconds(LocalDateTime.now());

            len = responseBody.length();

            logger.info("http報文響應正文長度:{}B", len);

            String extractedTitle = NetUtil.extractTitle(responseBody);

            return builder.withRespEnd(respEnd).withRespLen(len)
                    .withHttpStatus((short) resp.getStatusLine().getStatusCode())
                    .withRetrieveTitle(extractedTitle)
                    .withTitleMatched(StringUtils.equals(domain.getTitle(), extractedTitle))
                    .build();


        } catch (IOException e) {
}

之后的代碼

try {

            CloseableHttpClient httpClient = buildDefaultHttpClientTrustSSL();//信任證書

            String url = domain.getUrl();
            HttpGet httpGet = new HttpGet(url);
            httpGet.addHeader("User-Agent", NetUtil.INSPECTOR_USER_AGENT);
            httpGet.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            httpGet.addHeader("Accept-Encoding", "gzip, deflate");
            httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.9");

            CloseableHttpResponse resp = httpClient.execute(httpGet);

            String responseBody = EntityUtils.toString(resp.getEntity(), "utf-8");

            respEnd = DateUtil.toEpochMilliseconds(LocalDateTime.now());

            len = responseBody.length();

            logger.info("http報文響應正文長度:{}B", len);

            String extractedTitle = NetUtil.extractTitle(responseBody);

            return builder.withRespEnd(respEnd).withRespLen(len)
                    .withHttpStatus((short) resp.getStatusLine().getStatusCode())
                    .withRetrieveTitle(extractedTitle)
                    .withTitleMatched(StringUtils.equals(domain.getTitle(), extractedTitle))
                    .build();


        } catch (IOException e) {
}
 /**
     * 信任SSL證書
     * @return
     */
    public static CloseableHttpClient buildDefaultHttpClientTrustSSL()
    {
        SSLContext sslContext = null;
        try {
            sslContext = SSLContextBuilder.create().useProtocol(SSLConnectionSocketFactory.SSL).loadTrustMaterial((x, y) -> true).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        RequestConfig config = RequestConfig.custom()
                .setSocketTimeout(30000)
                .setConnectTimeout(30000)
                .setConnectionRequestTimeout(30000)
                .setContentCompressionEnabled(true)
                .build();
        return HttpClientBuilder.create().setDefaultRequestConfig(config).setSSLContext(sslContext).setSSLHostnameVerifier((x, y) -> true).build();
    }

 public static CloseableHttpClient buildDefaultHttpClient()
    {
        RequestConfig defaultRequestConfig = RequestConfig.custom()
                .setSocketTimeout(30000)
                .setConnectTimeout(30000)
                .setConnectionRequestTimeout(30000)
                .setContentCompressionEnabled(true)
                .setStaleConnectionCheckEnabled(true)
                .build();

        return HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 異常解決:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target Maven:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target mvn 編譯報錯mavn sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested targ 大坑!maven的web項目初始化錯誤:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification ... 報錯PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target" PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target maven PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path 解決 java 使用ssl過程中出現"PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target" nginx中配置wss的websocket報錯:PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 解決mvn package遇到sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target報錯問題
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM