有的新手彥祖在搬磚過程中會(huì)遇到調(diào)用別人接口來獲取數(shù)據(jù)的需求,這其中涉及調(diào)用一些相關(guān)類及方法的調(diào)用,最近干活又要用這個(gè)了,把以前的代碼搬出來套用下,死活報(bào)錯(cuò)協(xié)議加密證書啥的問題,真想感嘆卑微打工仔掙點(diǎn)錢養(yǎng)家糊口不容易。無所謂,我?guī)煾禃?huì)出手,過來一眼就看出我的問題,我用的http協(xié)議寫的,人家接口測(cè)試環(huán)境是https協(xié)議寫的,https協(xié)議有加密方式,去參照大佬的寫法封裝整理了兩個(gè)類。開發(fā)差不多完成后在這記錄分享下,不多bb直接上源碼了。
package com.inspur.dehongtf.controller.http_data_get;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Map;
public class httpsRequest {
public JSONObject send2(String url, Map<String, Object> params) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.ALL));
HttpEntity<Object> entity = new HttpEntity<>(JSONObject.toJSONString(params), headers);
try {
String body = getRestTemplate().postForObject(url, entity, String.class);
return JSONObject.parseObject(body);
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static RestTemplate getRestTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { return true;
}
}).build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext,
new String[]{"TLSv1"},
null,
NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(csf)
.build();
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
return restTemplate;
}
}
以上是一個(gè)自己再次封裝的用于調(diào)用https協(xié)議的類,send2方法調(diào)用了getRestTemplate方法,在測(cè)試類中調(diào)用時(shí)傳入兩個(gè)參數(shù),其一是String類型的url,其二是指定泛型的map集合,也就是post的請(qǐng)求參數(shù),對(duì)了,這個(gè)方法目前只適用于post方法。
如果有彥祖在看,順便給彥祖?zhèn)儨y(cè)試下



這里定義并書寫了參數(shù)和url,實(shí)例化上面的httpsRequest類,傳入url和請(qǐng)求參數(shù)調(diào)用send2方法,得到的結(jié)果就是在url接口指定參數(shù)下的數(shù)據(jù),下面輸出看一下

在控制層調(diào)用上面類的getParams方法,得到返回?cái)?shù)據(jù),啟動(dòng)工程,postman調(diào)用自己的接口,輸出看一下數(shù)據(jù),沒問題。文章來源:http://www.zghlxwxcb.cn/news/detail-535799.html

這里我也寫了一個(gè)http的類,但是好像https這個(gè)類能共用http與https,深層原理咱也不知道,http類如下,難免以后找不到了,又被同事說:多刪幾個(gè)妹子,不要?jiǎng)h代碼。文章來源地址http://www.zghlxwxcb.cn/news/detail-535799.html
package com.inspur.dehongtf.controller.http_data_get;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
public class httpRequest {
public JSONObject send1(String url, Map<String, Object> params) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
RestTemplate restTemplate = new RestTemplate();
JSONObject obj = new JSONObject(params);
String s = obj.toString();
HttpEntity request = new HttpEntity(s, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class, new Object[0]);
JSONObject responseData = JSONObject.parseObject((String)responseEntity.getBody());
return responseData;
}
}
到了這里,關(guān)于Springboot調(diào)用http(https)接口小妙招,新手小白版的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!