小白问题请教:SSL:Certificate for <IP> doesn't match any of the

[复制链接]
查看11 | 回复2 | 2021-1-27 06:44:14 | 显示全部楼层 |阅读模式
本人小白,请教大牛;采用的是HTTPSPost请求,使用Postman工具测试过能链接,采用的是IP直连方式。请各位大牛大哥解答一下这个问题出现的情况是什么原因引起?
以下是代码以及错误截图
publicstaticStringsend(Stringurl,Mapmap,Stringencoding,InputStreamcertStream,Stringpwd)throwsIOException{
Stringbody="";
SSLContextsslcontext=null;
try{
KeyStoretrustStore=KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(certStream,pwd.toCharArray());
//相信自己的CA和所有自签名的证书
sslcontext=SSLContexts.custom().loadTrustMaterial(trustStore,newTrustSelfSignedStrategy()).build();
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
//证书io流关闭
certStream.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
//设置协议http和https对应的处理socket链接工厂的对象
RegistrysocketFactoryRegistry=RegistryBuilder.create().register("http",PlainConnectionSocketFactory.INSTANCE).register("https",newSSLConnectionSocketFactory(sslcontext)).build();
PoolingHttpClientConnectionManagerconnManager=newPoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClients.custom().setConnectionManager(connManager);
//创建自定义的httpclient对象
CloseableHttpClientclient=HttpClients.custom().setConnectionManager(connManager).build();
//创建post方式请求对象
HttpPosthttpPost=newHttpPost(url);
//装填参数
Listnvps=newArrayList();
if(map!=null){
for(Map.Entryentry:map.entrySet()){
nvps.add(newBasicNameValuePair(entry.getKey(),entry.getValue()));
}
}
//设置参数到请求对象中
httpPost.setEntity(newUrlEncodedFormEntity(nvps,encoding));
System.out.println("请求地址:"+url);
//System.out.println("请求参数:"+nvps.toString());
//设置header信息
//指定报文头【Content-type】、【User-Agent】
httpPost.setHeader("Accept","application/json");
//httpPost.setHeader("User-Agent","Mozilla/4.0(compatible;MSIE5.0;WindowsNT;DigExt)");
//执行请求操作,并拿到结果(同步阻塞)
CloseableHttpResponseresponse=null;
try{
response=client.execute(httpPost);
//获取结果实体
HttpEntityentity=response.getEntity();
if(entity!=null){
//按指定编码转换结果实体为String类型
body=EntityUtils.toString(entity,encoding);
}
EntityUtils.consume(entity);
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
//释放链接
response.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
returnbody;
}


分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:44:14 | 显示全部楼层
问题分析:
颁发的证书是给制定域名的而非IP地址,因为服务器没有加入PublicDNS,所以域名无法访问,我在调用接口时直接使用了IP地址,而不是域名。
问题解决:
在hosts配置文件中添加IP地址和域名的映射记录,在调用接口时使用域名进行调用,发现问题解决。
https://www.cnblogs.com/xusweeter/p/7659953.html
回复

使用道具 举报

千问 | 2021-1-27 06:44:14 | 显示全部楼层
引用1楼LCL_data的回复:问题分析:
颁发的证书是给制定域名的而非IP地址,因为服务器没有加入PublicDNS,所以域名无法访问,我在调用接口时直接使用了IP地址,而不是域名。
问题解决:
在hosts配置文件中添加IP地址和域名的映射记录,在调用接口时使用域名进行调用,发现问题解决。
https://www.cnblogs.com/xusweeter/p/7659953.html


这个方法我试过了,修改hosts文件利用域名还是报一样的错误。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行