1.使用HttpClient4.3 调用https出现如下错误:

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

当使用网上其他的方式的时候,出现错误:javax.net.ssl.SSLException: hostname in certificate didn't match: <openapi.ysepay.com> != <default.ssl.cdn.jiasule.com>

原因:这是SSL证书请求问题。

2.原来的代码:

     /**
* 拼接请求参数,发起请求
* @param request
* @param sParaTemp
* @param strMethod
* @param strButtonName
* @return
*/
public static String sendRequest(String mch_id,HttpServletRequest request, Map<String, String> paraTemp) {
String result = null;// 返回的结果
CloseableHttpResponse response = null;
CloseableHttpClient client = null; HttpPost httpPost = new HttpPost(SwiftpassConfig.yinsheng_YSEPAY_GATEWAY_URL); //创建HttpPost对象
// 存参列表
List <NameValuePair> params = new ArrayList<NameValuePair>();
// 参数不为空
if(!paraTemp.isEmpty()) {
// 遍历map,保存到List中
for (Map.Entry<String, String> entry : paraTemp.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
try {
httpPost.setEntity(new UrlEncodedFormEntity(params ,HTTP.UTF_8));
// 创建 CloseableHttpClient 对象
client = HttpClients.createDefault();
response = client.execute(httpPost);
if(response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
//取出应答字符串
result = EntityUtils.toString(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
result = e.getMessage().toString();
}
}
return result;
}

使用上诉代码调用https接口,出现上述的错误。

3.修改之后的代码:

    /**
* buildSSLCloseableHttpClient:(设置允许所有主机名称都可以,忽略主机名称验证)
* @author xbq
* @return
* @throws Exception
*/
private static CloseableHttpClient buildSSLCloseableHttpClient() throws Exception {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
// 信任所有
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
// ALLOW_ALL_HOSTNAME_VERIFIER:这个主机名验证器基本上是关闭主机名验证的,实现的是一个空操作,并且不会抛出javax.net.ssl.SSLException异常。
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1" }, null,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} /**
* 拼接请求参数,发起请求
* @param request
* @param sParaTemp
* @param strMethod
* @param strButtonName
* @return
*/
public static String sendRequest(String mch_id,HttpServletRequest request, Map<String, String> paraTemp) {
String result = null;// 返回的结果
CloseableHttpResponse response = null;
CloseableHttpClient client = null; HttpPost httpPost = new HttpPost(SwiftpassConfig.yinsheng_YSEPAY_GATEWAY_URL); //创建HttpPost对象
// 存参列表
List <NameValuePair> params = new ArrayList<NameValuePair>();
// 参数不为空
if(!paraTemp.isEmpty()) {
// 遍历map,保存到List中
for (Map.Entry<String, String> entry : paraTemp.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
try {
httpPost.setEntity(new UrlEncodedFormEntity(params ,HTTP.UTF_8));
// 调用方法,创建 CloseableHttpClient 对象
client = buildSSLCloseableHttpClient();
response = client.execute(httpPost);
if(response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
//取出应答字符串
result = EntityUtils.toString(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
result = e.getMessage().toString();
}
}
return result;
}

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed的更多相关文章

  1. javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certificatio

    场景:Java调用PHP接口,代码部署在服务器上后,调用报错,显示PHP服务器那边证书我这边服务器不信任(我猜的). 异常信息: 2019-08-06 14:00:09,102 [http-nio-4 ...

  2. 异常信息:javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed

    上周五遇到一个问题,工程本地编译运行正常,打包本地tomcat运行也正常.部署到测试环境报错: 2017-05-05 09:38:11.645 ERROR [HttpPoolClientsUtil.j ...

  3. javax.net.ssl.sslhandshakeException:sun.security.validator.validatorException:PKIX path buildind failed

    前段时间开发的一个需求,需要通过图片URL获取图片的base64编码,测试的时候使用的是百度图片的url,测试没有问题,但是发布后测试时报如下错: javax.net.ssl.sslhandshake ...

  4. 抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法

    抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法 原因是https证书问题, ...

  5. 解决 sun.security.validator.ValidatorException: PKIX path building failed

    今天用java HttpClients写爬虫在访问某Https站点报如下错误: sun.security.validator.ValidatorException: PKIX path buildin ...

  6. 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: ...

  7. 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 ...

  8. javax.net.ssl.SSLHandshakeException sun.security.validator.ValidatorException PK

    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building f ...

  9. 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 = build ...

随机推荐

  1. 安卓程序代写 网上程序代写[原]BluetoothAdapter解析

    这篇文章将会详细解析BluetoothAdapter的详细api, 包括隐藏方法, 每个常量含义. 一 BluetoothAdapter简介 1.继承关系 该类仅继承了Object类; 2.该类作用 ...

  2. C++ StringBuilder类

    #ifndef __GTL_STRINGBUILDER_H_ #define __GTL_STRINGBUILDER_H_ #include <list> #include <str ...

  3. Python 类的多态

    #python的多态 class Dog(): def eat(self): print("i am dog , eat something . ") class Cat(): d ...

  4. nano 命令 linux

    用途说明 nano是一个字符终端的文本编辑器,有点像DOS下的editor程序.它比vi/vim要简单得多,比较适合Linux初学者使用.某些Linux发行版的默认编辑器就是nano.(nano - ...

  5. paoding分词

    Paoding 详细介绍 庖丁中文分词库是一个使用Java开发的,可结合到Lucene应用中的,为互联网.企业内部网使用的中文搜索引擎分词组件.Paoding填补了国内中文分词方面开源组件的空白,致力 ...

  6. 【4】JVM-GC设计思路分析

    Java中将内存的控制交给JVM来实现,方便了JAVA程序猿,当然牺牲了一部分效率,不过总体来看是值得的.那么JVM中是如何设计GC的呢,本文从几个问题入手,然后分析了一下设计思路,如果有理解错误的地 ...

  7. MBR:主引导记录:

    下面内容严重参考:百度百科: Main Boot Record)是位于磁盘最前边的一段引导(Loader)代码.它负责磁盘操作系统(DOS)对磁盘进行读写时分区合法性的判别.分区引导信息的定位,它由磁 ...

  8. Deep Residual Learning for Image Recognition这篇文章

    作者:何凯明等,来自微软亚洲研究院: 这篇文章为CVPR的最佳论文奖:(conference on computer vision and pattern recognition) 在神经网络中,常遇 ...

  9. e792. 建立一个包括所有数据的SpinnerListModel

    By default, if the user is browsing the values in a SpinnerListModel, the iteration stops when eithe ...

  10. [转]ESP8266使用详解

    本文转自:yangfengwu 原文地址:http://www.cnblogs.com/yangfengwu/p/5205570.html 这两天测试发现一个地方写错了 发送数据的地方 还发现,自己用 ...