Java發(fā)送https請求并跳過ssl證書驗(yàn)證方法
平臺服務(wù)調(diào)用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
第一種方法:配置相關(guān)SSL證書到服務(wù)器
第二種方法:如果沒有相關(guān)服務(wù)器權(quán)限,又想快速驗(yàn)證接口調(diào)用,可以在請求時添加跳過SSL證書,可以 快捷實(shí)現(xiàn),當(dāng)然生產(chǎn)環(huán)境還是建議配置證書方式,降低風(fēng)險
/**
* 發(fā)送https請求并跳過ssl證書驗(yàn)證
* 條件:請求體格式為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();
//創(chuàng)建httpclient對象
CloseableHttpClient client = null;
String respBody;
client = HttpClients.custom().setSSLSocketFactory(new SSLConnectionSocketFactory(SSLContexts.custom()
.loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), NoopHostnameVerifier.INSTANCE)).build();
//創(chuàng)建post方式請求對象
HttpPost httpPost = new HttpPost(url);
// 請求頭設(shè)置
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直接調(diào)用HTTP接口,并且獲取List出參,輸出數(shù)據(jù)List
1.代碼實(shí)現(xiàn)
public WrapperResponse<List<WarningDTO>> queryBigWarning(WarnInfoBInfoQueryDTO warnInfoBInfoQueryDTO) throws Exception {
String SERVICE_URL = "http://127.0.0.1:8889/wakljfa";
//發(fā)送httpPost請求
//創(chuàng)建HttpClient
HttpClient httpclient = HttpClients.createDefault();
//發(fā)送接口地址
HttpPost httppost = new HttpPost(SERVICE_URL);
//定義String請求Json參數(shù)體
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");
//發(fā)送請求并接收response
HttpResponse httpresponse = httpclient.execute(httppost);
String result = EntityUtils.toString(httpresponse.getEntity(), "UTF-8");
ObjectMapper objectMapper = new ObjectMapper();
JsonNode responseJson = objectMapper.readTree(result);
// 從JSON對象中獲取鍵值對,根據(jù)出參格式獲取出參數(shù)據(jù)
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"
}
]
}總結(jié)
到此這篇關(guān)于Java發(fā)送https請求并跳過ssl證書驗(yàn)證方法的文章就介紹到這了,更多相關(guān)發(fā)送https請求跳過ssl證書內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?嵌入數(shù)據(jù)引擎從?SQLite?到?SPL詳解
這篇文章主要介紹了Java?嵌入數(shù)據(jù)引擎:從?SQLite?到?SPL,SQLite架構(gòu)簡單,其核心雖然是C語言開發(fā)的,但封裝得比較好,對外呈現(xiàn)為一個小巧的Jar包,能方便地集成在Java應(yīng)用中,本文給大家介紹的非常詳細(xì),需要的朋友參考下2022-07-07
Java加載properties文件實(shí)現(xiàn)方式詳解
這篇文章主要介紹了Java加載properties文件實(shí)現(xiàn)方式詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
java 進(jìn)制轉(zhuǎn)換實(shí)例詳解
這篇文章主要介紹了java 進(jìn)制轉(zhuǎn)換實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
Java的Hibernate框架中的雙向主鍵關(guān)聯(lián)與雙向外鍵關(guān)聯(lián)
Hibernate想要實(shí)現(xiàn)雙向的關(guān)聯(lián)就必須在映射文件的兩端同時配置<one-to-one>,另外還要在主映射的一端采用foreign外鍵關(guān)聯(lián)屬性,下面我們就一起來看一下Java的Hibernate框架中的雙向主鍵關(guān)聯(lián)與雙向外鍵關(guān)聯(lián)方法:2016-06-06
Spring中@ControllerAdvice注解的用法解析
這篇文章主要介紹了Spring中@ControllerAdvice注解的用法解析,顧名思義,@ControllerAdvice就是@Controller 的增強(qiáng)版,@ControllerAdvice主要用來處理全局?jǐn)?shù)據(jù),一般搭配@ExceptionHandler、@ModelAttribute以及@InitBinder使用,需要的朋友可以參考下2023-10-10
@RequestBody,@RequestParam和@Param的區(qū)別說明
這篇文章主要介紹了@RequestBody,@RequestParam和@Param的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

