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();

	}

本篇文章只为提供一个简洁的实现代码,就不添加注释了。

功能很简单,希望有帮到你。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值