javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed
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的更多相关文章
- 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 ...
- 异常信息:javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed
上周五遇到一个问题,工程本地编译运行正常,打包本地tomcat运行也正常.部署到测试环境报错: 2017-05-05 09:38:11.645 ERROR [HttpPoolClientsUtil.j ...
- javax.net.ssl.sslhandshakeException:sun.security.validator.validatorException:PKIX path buildind failed
前段时间开发的一个需求,需要通过图片URL获取图片的base64编码,测试的时候使用的是百度图片的url,测试没有问题,但是发布后测试时报如下错: javax.net.ssl.sslhandshake ...
- 抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法
抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法 原因是https证书问题, ...
- 解决 sun.security.validator.ValidatorException: PKIX path building failed
今天用java HttpClients写爬虫在访问某Https站点报如下错误: sun.security.validator.ValidatorException: PKIX path buildin ...
- 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: ...
- 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 ...
- javax.net.ssl.SSLHandshakeException sun.security.validator.ValidatorException PK
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building f ...
- 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 ...
随机推荐
- android批量插入数据效率对比
对比在android中批量插入数据的3中方式对比(各插入1W条数据所花费的时间): 1. 一个一个插入 /** * 向表中插入数据 * * @param openHelper * @param app ...
- Ilya Sutskever
中文名:伊利亚 莎士科尔 早期: 加拿大多伦多大学,Hinton实验室,2005年至2012年 博士后: 斯坦福大学,Andrew实验室,2012年11月到2012年12月 工作: 谷歌,2013年3 ...
- (笔记)Linux Root下的.gvfs出现异常解决办法
在linux系统下安装软件或复制文件的时候,复制不成功,出现错误如下: error: failed to stat /home/dade/.gvfs: Permission denied. 表面上看: ...
- 第三百五十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—将bloomfilter(布隆过滤器)集成到scrapy-redis中
第三百五十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—将bloomfilter(布隆过滤器)集成到scrapy-redis中,判断URL是否重复 布隆过滤器(Bloom Filter)详 ...
- e791. 为JSpinner定制编辑器
This example replaces the default editor (a JFormattedTextField) in a spinner component with a custo ...
- 使用ClaimsIdentity来实现登录授权
背景:以前做登录时用的都是FormsAuthentication.SetAuthCookie(model.UID, IsRemeber),但是有一个不好,不能存储多个值,有时候我们既想存储登录用户的U ...
- 浅析Android Camera开发中的三个尺寸和三种变形 (贡献一个自适配Picturesize和Previewsize的工具类)
转至 (http://blog.csdn.net/yanzi1225627/article/details/17652643) 经常听人问Camera开发中,各种变形问题,今天有空就在此梳理总结下. ...
- 一款标注颜色,距离的小软件 markman
长度标记 坐标和矩形标记 色值标记 文字标记 长度自动测量 标记拖拽删除 支持多种图片格式 支持PSD(需用最大兼容保存).PNG.BMP.JPG格式 设计稿自动刷新 在标注的 ...
- linux下时间同步的两种方法分享
方法1:与一个已知的时间服务器同步 复制代码 代码如下: ntpdate time.nist.gov 其中 time.nist.gov 是一个时间服务器. 删除本地时间并设置时区为上海 复制代码 代码 ...
- 本地没问题 服务器 提示 Server Error in '/' Application
一. 先用本机的 IIS 测试,不要用 VS 内附的 Web server,並配置 <customErrors mode="Off"/>,以將真實的錯誤原因顯示出來,看 ...