广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >发送https请求并跳过ssl证书验证
  • 519
分享到

发送https请求并跳过ssl证书验证

httpsssljava 2023-09-09 14:09:53 519人浏览 独家记忆
摘要

 平台服务调用https接口报错: org.springframework.WEB.client.ResourceAccessException: I/0 error on PoST request for ?"Https://XXXX

 平台服务调用https接口报错:

org.springframework.WEB.client.ResourceAccessException: I/0 error on PoST request for ?"Https://XXXXX": java.security.centp.CertificateException: No subject alternative names present; nested exception is javax.net.ssl..SSLHandshakeException: java.security.cert.CertificateException: No subject alternative namesspresent

第一种方法:配置相关SSL证书到服务器

第二种方法:如果没有相关服务器权限,又想快速验证接口调用,可以在请求时添加跳过SSL证书,可以 快捷实现,当然生产环境还是建议配置证书方式,降低风险

    public static String sendAskSkipSSLCertificate(String url, Map body, Map header) throws Exception {        CloseableHttpResponse response = null;        // 处理请求路径        url = UriComponentsBuilder.fromHttpUrl(url) .toUriString();        //创建httpclient对象        CloseableHttpClient client = null;        String respBody;        client = HttpClients.custom().setSSLSocketFactory(new SSLConnectionSocketFactory(SSLContexts.custom()                        .loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), NoopHostnameVerifier.INSTANCE)).build();        //创建post方式请求对象        HttpPost httpPost = new HttpPost(url);        // 请求头设置        httpPost.setHeader("Content-Type", "application/JSON");        if (header != null) {            for (String s : header.keySet()) {                httpPost.setHeader(s, header.get(s));            }        }        if (body != null) {            httpPost.setEntity(new StringEntity(jsON.toJSONString(body), "utf-8"));        }        response = client.execute(httpPost);        org.apache.http.HttpEntity entity = response.getEntity();        if (entity != null) {            respBody = EntityUtils.toString(entity);            return respBody;        }        return null; }

来源地址:https://blog.csdn.net/Zxiaobinggan/article/details/129306508

--结束END--

本文标题: 发送https请求并跳过ssl证书验证

本文链接: https://www.lsjlt.com/news/401569.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作