有的新手彦祖在搬砖过程中会遇到调用别人接口来获取数据的需求,这其中涉及调用一些相关类及方法的调用,最近干活又要用这个了,把以前的代码搬出来套用下,死活报错协议加密证书啥的问题,真想感叹卑微打工仔挣点钱养家糊口不容易。无所谓,我师傅会出手,过来一眼就看出我的问题,我用的http协议写的,人家接口测试环境是https协议写的,https协议有加密方式,去参照大佬的写法封装整理了两个类。开发差不多完成后在这记录分享下,不多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;
}
}
以上是一个自己再次封装的用于调用https协议的类,send2方法调用了getRestTemplate方法,在测试类中调用时传入两个参数,其一是String类型的url,其二是指定泛型的map集合,也就是post的请求参数,对了,这个方法目前只适用于post方法。
如果有彦祖在看,顺便给彦祖们测试下
这里定义并书写了参数和url,实例化上面的httpsRequest类,传入url和请求参数调用send2方法,得到的结果就是在url接口指定参数下的数据,下面输出看一下
在控制层调用上面类的getParams方法,得到返回数据,启动工程,postman调用自己的接口,输出看一下数据,没问题。文章来源:https://www.toymoban.com/news/detail-535799.html
这里我也写了一个http的类,但是好像https这个类能共用http与https,深层原理咱也不知道,http类如下,难免以后找不到了,又被同事说:多删几个妹子,不要删代码。文章来源地址https://www.toymoban.com/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;
}
}
到了这里,关于Springboot调用http(https)接口小妙招,新手小白版的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!