Java發(fā)送https請求并跳過ssl證書驗證方法
平臺服務(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)限,又想快速驗證接口調(diào)用,可以在請求時添加跳過SSL證書,可以 快捷實現(xiàn),當(dāng)然生產(chǎn)環(huán)境還是建議配置證書方式,降低風(fēng)險
/**
* 發(fā)送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();
//創(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.代碼實現(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證書驗證方法的文章就介紹到這了,更多相關(guān)發(fā)送https請求跳過ssl證書內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA發(fā)送HTTP請求的多種方式詳細總結(jié)
目前做項目中有一個需求是這樣的,需要通過Java發(fā)送url請求,查看該url是否有效,這時我們可以通過獲取狀態(tài)碼來判斷,下面這篇文章主要給大家介紹了關(guān)于JAVA發(fā)送HTTP請求的多種方式總結(jié)的相關(guān)資料,需要的朋友可以參考下2023-01-01
Mybatis中isNotNull與isNotEmpty的使用心得
這篇文章主要介紹了Mybatis中isNotNull與isNotEmpty的使用心得,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
關(guān)于通過Java連接mysql對反斜杠”\“轉(zhuǎn)義的測試詳解
這篇文章主要給大家介紹了關(guān)于通過Java連接mysql對反斜杠”\“轉(zhuǎn)義的測試的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家理解反斜杠”\“轉(zhuǎn)義具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-06-06
使用 Redis 緩存實現(xiàn)點贊和取消點贊的示例代碼
這篇文章主要介紹了使用 Redis 緩存實現(xiàn)點贊和取消點贊的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
spring boot與spring mvc的區(qū)別及功能介紹
這篇文章主要介紹了spring boot與spring mvc的區(qū)別是什么以及spring boot和spring mvc功能介紹,感興趣的朋友一起看看吧2018-02-02
Mybatis-Plus通過SQL注入器實現(xiàn)批量插入的實踐
本文主要介紹了Mybatis-Plus通過SQL注入器實現(xiàn)批量插入的實踐,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

