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();
}

sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target的更多相关文章

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

    还是记录使用 maven 时遇到的问题. 一.maven报错 maven package 进行打包时出现了以下报错: Non-resolvable parent POM for com.wpbxin: ...

  2. mvn 编译报错mavn sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested targ

    mavn 编译报错: mavn sun.security.validator.ValidatorException: PKIX path building failed: sun.security.p ...

  3. 解决 java 使用ssl过程中出现"PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"

    今天,封装HttpClient使用ssl时报一下错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExc ...

  4. java程序中访问https时,报 PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    在java中使用https访问数据时报异常: Caused by: sun.security.validator.ValidatorException: PKIX path building fail ...

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

    注:网上搜来的快照,暂未验证 在java代码中请求https链接的时候,可能会报下面这个错误javax.net.ssl.SSLHandshakeException: sun.security.vali ...

  6. Flutter配置环境报错“PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target”

    背景:最近看了很多Flutter漂亮的项目,想要尝试一下.所有环境都搭建好之后,按照文档一步一步配置(抄袭),但始终报如下图错误. PKIX path building failed: sun.sec ...

  7. ES访问遇到sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    cmd命令cd到jre/bin目录下 输入命令keytool -import -alias 别名 -keystore cacerts -file ‪C://certs//elasticsearch// ...

  8. maven PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path

    maven编译的时候遇到的奇葩问题,  非常奇葩, 所有其他同事都没有遇到 , 仅仅是我遇到了 不清楚是因为用了最新的JDK的缘故(1.8 update91)还是其他什么原因. 总之是证书的问题. 当 ...

  9. 报错PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"

    今天在调用第三方HTTPS接口的时候,一直显示这个报错,然后百度很久,有2种解决方法,一个是说自己手动去导入,第二种用代码忽略证书验证.我用二种方式, 复制即用, public void test2( ...

随机推荐

  1. vmware 兼容问题

    1.出现此问题的原因是Device Guard或Credential Guard与Workstation不兼容.2.Windows系统的Hyper-V不兼容导致.解决方法: 步骤一:禁用Device ...

  2. 数据防泄密(DLP)

    数据防泄密(DLP)近几年已经成为国内非常热门的关键词. 目前市场上DLP产品主要可以分为三大类,各类产品都来源于不同的技术体系,防护效果也各有优缺点. 第一类,以监控审计为主,对进出的数据进行过滤, ...

  3. java线程基础巩固---Daemon线程的创建以及使用场景分析

    daemon线程既守护线程,而在jdk中对于Thread中针对守护线程有专门的API,如下: 而之前在公司项目中就看到过有人使用过Thread中的这个API,但是对于它的使用场景完全不知,所以这次好好 ...

  4. SAP导出内表数据到excel

    DATA: EXCEL    TYPE OLE2_OBJECT,      SHEET    TYPE OLE2_OBJECT,      CELL     TYPE OLE2_OBJECT,     ...

  5. golang高并发

    golang 为什么能做到高并发 goroutine是go并行的关键,goroutine说到底就是携程,但是他比线程更小,几十个goroutine可能体现在底层就是五六个线程,Go语言内部帮你实现了这 ...

  6. Java通过腾讯邮箱发送邮件

    private static void sendEmaill() { Properties prop = new Properties(); //协议 prop.setProperty("m ...

  7. springboot2.1.7整合Druid

    一.maven的依赖:文中就贴重点的, 其他依赖就不贴了 <dependency> <groupId>com.alibaba</groupId> <artif ...

  8. 不使用C库函数(Sprintf)将void* 指针转换为十六进制字符串

    #include <stdio.h> #include <stdint.h> #include <stdlib.h> void hexDump(void *ptr, ...

  9. PHP swoole UDP服务端和客户端

    服务端 <?php $serv = ,SWOOLE_PROCESS,SWOOLE_SOCK_UDP); $serv->on('Packet',function ($serv,$data,$ ...

  10. 【Winform-自定义控件】可以使用2种半透明的颜色来填充Button

    制作一个自定义按钮,使用2种半透明的颜色来填充Button 1.添加一个自定义控件类,并改变基类,继承自Button public partial class CustomControl1 : But ...