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

  1. 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.原来的代码:

  1. /**
  2. * 拼接请求参数,发起请求
  3. * @param request
  4. * @param sParaTemp
  5. * @param strMethod
  6. * @param strButtonName
  7. * @return
  8. */
  9. public static String sendRequest(String mch_id,HttpServletRequest request, Map<String, String> paraTemp) {
  10. String result = null;// 返回的结果
  11. CloseableHttpResponse response = null;
  12. CloseableHttpClient client = null;
  13.  
  14. HttpPost httpPost = new HttpPost(SwiftpassConfig.yinsheng_YSEPAY_GATEWAY_URL); //创建HttpPost对象
  15. // 存参列表
  16. List <NameValuePair> params = new ArrayList<NameValuePair>();
  17. // 参数不为空
  18. if(!paraTemp.isEmpty()) {
  19. // 遍历map,保存到List中
  20. for (Map.Entry<String, String> entry : paraTemp.entrySet()) {
  21. params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
  22. }
  23. try {
  24. httpPost.setEntity(new UrlEncodedFormEntity(params ,HTTP.UTF_8));
  25. // 创建 CloseableHttpClient 对象
  26. client = HttpClients.createDefault();
  27. response = client.execute(httpPost);
  28. if(response.getStatusLine().getStatusCode() == 200) {
  29. HttpEntity httpEntity = response.getEntity();
  30. //取出应答字符串
  31. result = EntityUtils.toString(httpEntity);
  32. }
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. result = e.getMessage().toString();
  36. }
  37. }
  38. return result;
  39. }

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

3.修改之后的代码:

  1. /**
  2. * buildSSLCloseableHttpClient:(设置允许所有主机名称都可以,忽略主机名称验证)
  3. * @author xbq
  4. * @return
  5. * @throws Exception
  6. */
  7. private static CloseableHttpClient buildSSLCloseableHttpClient() throws Exception {
  8. SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
  9. // 信任所有
  10. public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  11. return true;
  12. }
  13. }).build();
  14. // ALLOW_ALL_HOSTNAME_VERIFIER:这个主机名验证器基本上是关闭主机名验证的,实现的是一个空操作,并且不会抛出javax.net.ssl.SSLException异常。
  15. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1" }, null,
  16. SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  17. return HttpClients.custom().setSSLSocketFactory(sslsf).build();
  18. }
  19.  
  20. /**
  21. * 拼接请求参数,发起请求
  22. * @param request
  23. * @param sParaTemp
  24. * @param strMethod
  25. * @param strButtonName
  26. * @return
  27. */
  28. public static String sendRequest(String mch_id,HttpServletRequest request, Map<String, String> paraTemp) {
  29. String result = null;// 返回的结果
  30. CloseableHttpResponse response = null;
  31. CloseableHttpClient client = null;
  32.  
  33. HttpPost httpPost = new HttpPost(SwiftpassConfig.yinsheng_YSEPAY_GATEWAY_URL); //创建HttpPost对象
  34. // 存参列表
  35. List <NameValuePair> params = new ArrayList<NameValuePair>();
  36. // 参数不为空
  37. if(!paraTemp.isEmpty()) {
  38. // 遍历map,保存到List中
  39. for (Map.Entry<String, String> entry : paraTemp.entrySet()) {
  40. params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
  41. }
  42. try {
  43. httpPost.setEntity(new UrlEncodedFormEntity(params ,HTTP.UTF_8));
  44. // 调用方法,创建 CloseableHttpClient 对象
  45. client = buildSSLCloseableHttpClient();
  46. response = client.execute(httpPost);
  47. if(response.getStatusLine().getStatusCode() == 200) {
  48. HttpEntity httpEntity = response.getEntity();
  49. //取出应答字符串
  50. result = EntityUtils.toString(httpEntity);
  51. }
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. result = e.getMessage().toString();
  55. }
  56. }
  57. return result;
  58. }

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. android批量插入数据效率对比

    对比在android中批量插入数据的3中方式对比(各插入1W条数据所花费的时间): 1. 一个一个插入 /** * 向表中插入数据 * * @param openHelper * @param app ...

  2. Ilya Sutskever

    中文名:伊利亚 莎士科尔 早期: 加拿大多伦多大学,Hinton实验室,2005年至2012年 博士后: 斯坦福大学,Andrew实验室,2012年11月到2012年12月 工作: 谷歌,2013年3 ...

  3. (笔记)Linux Root下的.gvfs出现异常解决办法

    在linux系统下安装软件或复制文件的时候,复制不成功,出现错误如下: error: failed to stat /home/dade/.gvfs: Permission denied. 表面上看: ...

  4. 第三百五十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—将bloomfilter(布隆过滤器)集成到scrapy-redis中

    第三百五十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—将bloomfilter(布隆过滤器)集成到scrapy-redis中,判断URL是否重复 布隆过滤器(Bloom Filter)详 ...

  5. e791. 为JSpinner定制编辑器

    This example replaces the default editor (a JFormattedTextField) in a spinner component with a custo ...

  6. 使用ClaimsIdentity来实现登录授权

    背景:以前做登录时用的都是FormsAuthentication.SetAuthCookie(model.UID, IsRemeber),但是有一个不好,不能存储多个值,有时候我们既想存储登录用户的U ...

  7. 浅析Android Camera开发中的三个尺寸和三种变形 (贡献一个自适配Picturesize和Previewsize的工具类)

    转至 (http://blog.csdn.net/yanzi1225627/article/details/17652643) 经常听人问Camera开发中,各种变形问题,今天有空就在此梳理总结下. ...

  8. 一款标注颜色,距离的小软件 markman

    长度标记   坐标和矩形标记   色值标记   文字标记   长度自动测量   标记拖拽删除   支持多种图片格式 支持PSD(需用最大兼容保存).PNG.BMP.JPG格式 设计稿自动刷新 在标注的 ...

  9. linux下时间同步的两种方法分享

    方法1:与一个已知的时间服务器同步 复制代码 代码如下: ntpdate time.nist.gov 其中 time.nist.gov 是一个时间服务器. 删除本地时间并设置时区为上海 复制代码 代码 ...

  10. 本地没问题 服务器 提示 Server Error in '/' Application

    一. 先用本机的 IIS 测试,不要用 VS 内附的 Web server,並配置 <customErrors mode="Off"/>,以將真實的錯誤原因顯示出來,看 ...