平台服务调用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 subjec编程客栈t 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 = UriComponwww.devze.comentsBuilder.fromHttpUrl(url) .toUriString();
//创建httpclient对象
CloseableHttpClient client = null;
String respBody;
client = HttpClients.custom().setSSLSocketFactory(new SSLConnectionSocketFactory(SSLContexts.custom()
.loadTrustMaterial(null, new TrustSelfSignedStrphpategy()).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();
pythonwarningDTO.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证书内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
加载中,请稍侯......
精彩评论