Java发送https请求并跳过ssl证书验证方法
平台服务调用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证书,可以 快捷实现,当然生产环境还是建议配置证书方式,降低风险
/** * 发送https请求并跳过ssl证书验证 * 条件:请求体格式为json * * @param url * @param body * @return */ public static String sendAskSkipSSLCertificate(String url, Map<String, Object> body, Map<String, String> 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; }
附:Java直接调用HTTP接口,并且获取List出参,输出数据List
1.代码实现
public WrapperResponse<List<WarningDTO>> queryBigWarning(WarnInfoBInfoQueryDTO warnInfoBInfoQueryDTO) throws Exception { String SERVICE_URL = "http://127.0.0.1:8889/wakljfa"; //发送httpPost请求 //创建HttpClient HttpClient httpclient = HttpClients.createDefault(); //发送接口地址 HttpPost httppost = new HttpPost(SERVICE_URL); //定义String请求Json参数体 httppost.setEntity(new StringEntity(new String("{" + "\"pageNum\": \"" + warnInfoBInfoQueryDTO.getPageNum() + "\"," + "\"pageSize\": \"" + warnInfoBInfoQueryDTO.getPageSize() + "\"," + "\"warnType\": \"" + warnInfoBInfoQueryDTO.getWarnType() + "\"" + "}"), Charset.forName("UTF-8"))); httppost.setHeader("Content-Type", "application/json"); //发送请求并接收response HttpResponse httpresponse = httpclient.execute(httppost); String result = EntityUtils.toString(httpresponse.getEntity(), "UTF-8"); ObjectMapper objectMapper = new ObjectMapper(); JsonNode responseJson = objectMapper.readTree(result); // 从JSON对象中获取键值对,根据出参格式获取出参数据 JsonNode data = responseJson.get("data"); JsonNode listWarn = pageInfo.get("list"); Iterator<JsonNode> iterator = listWarn.iterator(); List<WarningDTO> warningDTOS = new ArrayList<>(); while (iterator.hasNext()) { WarningDTO warningDTO = new WarningDTO(); JsonNode warningNode = iterator.next(); String warnOcurTime= warningNode.get("warnOcurTime").asText(); warningDTO.setWarnOcurTime(warnOcurTime); String warnId= warningNode.get("warnId").asText(); warningDTO.setWarnId( warnId); String id= warningNode.get("id").asText(); warningDTO.setId( Id); warningDTOS.add(warningDTO); } return WrapperResponse.success(warningDTOS); }
2.出参模式
"data": { "list": [ { "warnId": "000000000000000000000000000201", "warnOcurTime": 1672502400000, "id": "000000000000000000000000000201" }, { "warnId": "000000000000000000000000000301", "warnOcurTime": 1672502400000, "id": "000000000000000000000000000301" } ] }
总结
到此这篇关于Java发送https请求并跳过ssl证书验证方法的文章就介绍到这了,更多相关发送https请求跳过ssl证书内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
MyBatis的mapper.xml文件中入参和返回值的实现
这篇文章主要介绍了MyBatis的mapper.xml文件中入参和返回值的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-01-01IDEA2023.3.4开启SpringBoot项目的热部署(图文)
本文使用的开发工具是idea,使用的是springboot框架开发的项目,配置热部署,可以提高开发效率,文中通过图文介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧2024-02-02
最新评论