java后台发送https请求基于httpTemplate的httpUtil工具实现
目录
java后台发送https请求(基于httpTemplate的httpUtil工具实现)
最近做连续做了一些java后台发送http请求的需求,发现项目里实现http请求的写法各异,不够简洁统一,于是基于httpTemplate自行封装了一个http请求工具,常见的json和octet-stream返回类型都可以接收。
get请求,返回类型为octet-stream
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.SSLContext;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.Map;
public static ResponseEntity<String> sendGet(String url, Map<String,Object> params) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(csf)
.build();
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate template = new RestTemplate(requestFactory);
template.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
HttpHeaders httpHeaders = new HttpHeaders();
HttpMethod method = HttpMethod.GET;
String jsonParams = JSONObject.toJSONString(params, SerializerFeature.WriteMapNullValue);
httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
HttpEntity<Object> objectHttpEntity = new HttpEntity<>(jsonParams,httpHeaders);
ResponseEntity<String> exchange = template.exchange(url, method, objectHttpEntity, String.class);
return exchange;
}
post请求,返回类型为json
public static Map sendPost(String url, Map<String,Object> params) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(csf)
.build();
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate template = new RestTemplate(requestFactory);
HttpHeaders httpHeaders = new HttpHeaders();
HttpMethod method = HttpMethod.POST;
String jsonParams = JSONObject.toJSONString(params, SerializerFeature.WriteMapNullValue);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Object> objectHttpEntity = new HttpEntity<>(jsonParams,httpHeaders);
ResponseEntity<Map> exchange = template.exchange(url, method, objectHttpEntity, Map.class);
return exchange.getBody();
}
本篇文章只为提供一个简洁的实现代码,就不添加注释了。
功能很简单,希望有帮到你。