Certificate for <xx.xxx.xxx.xxx> doesn't match any of the subject alternative names: [xx.xxxx.xxxx.com]
1、 用HttpClient发送Https请求报SSLException: Certificate for <域名> doesn't match any of the subject alternative names问题的解决,报错,如下所示:
1 javax.net.ssl.SSLPeerUnverifiedException: Certificate for <xx.xxx.xxx.xxx> doesn't match any of the subject alternative names: [xx.xxxx.xxxx.com] 2 at org.apache.http.conn.ssl.SSLConnectionSocketFactory.verifyHostname(SSLConnectionSocketFactory.java:507) 3 at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:437) 4 at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:384) 5 at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142) 6 at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376) 7 at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393) 8 at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) 9 at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186) 10 at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) 11 at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) 12 at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) 13 at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) 14 at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
2、使用Apache HttpClient做https的Post请求,调用代码就报上面的错误。
代码参考,需要特别注意的是SSLContexts引入的httpcore-4.4.14.jar包,如下所示:
1 package com.xxx.main.httpclient; 2 3 import org.apache.http.conn.ssl.NoopHostnameVerifier; 4 import org.apache.http.conn.ssl.SSLConnectionSocketFactory; 5 import org.apache.http.conn.ssl.TrustSelfSignedStrategy; 6 import org.apache.http.impl.client.CloseableHttpClient; 7 import org.apache.http.impl.client.HttpClients; 8 import org.apache.http.ssl.SSLContexts; 9 10 public class HttpClientMain { 11 12 13 public static void testShared(String catalogId, String appKey, String secret) throws Exception { 14 CloseableHttpClient httpClient = null; 15 16 // 解决httpClient发送https错误的问题 17 SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory( 18 SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), 19 NoopHostnameVerifier.INSTANCE); 20 httpClient = HttpClients.custom().setSSLSocketFactory(scsf).build(); 21 22 // ....... 23 24 } 25 26 27 }
分类:
错误集锦
, HttpClient
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?