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();
    }
原文地址:https://www.cnblogs.com/passedbylove/p/11214671.html