springboot 访问httpclient接口

使用jar:

<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
	<version>4.5.4</version>
</dependency>

工具类:

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 
 * @title:  httpclient 工具类
 * @author: wll
 * @since:  2021-5-27 10:00:27
 */
public class HttpClientUtil {

	private static final int socketTimeout = 10 * 60 * 1000;// 请求超时时间十分钟
	private static final int connectTimeout = 10 * 60 * 1000;// 传输超时时间十分钟
	private static final String ENCODING = "UTF-8";// 编码格式。发送编码格式统一用UTF-8

	/**
	 * POST方式请求参数
	 * 
	 * @param url       请求地址
	 * @param heads     请求头
	 * @param jsonParam 请求参数
	 * @return
	 * @throws Exception
	 */
	public static String postFun(String url, Map<String, String> heads, String jsonParam) throws Exception {

		System.out.println("------>HttpClient POST DATA url:" + url + ",heads:" + heads + ",reqStr:" + jsonParam);

		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost post = new HttpPost(url);
		
		// 设置请求和传输超时时间
		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
		post.setConfig(requestConfig);

		// 设置头部参数
		if (heads != null && !heads.isEmpty()) {
			for (Map.Entry<String, String> entry : heads.entrySet()) {
				post.addHeader(entry.getKey(), entry.getValue());
			}
		}

		// 请求body参数
		if (StringUtils.isNotBlank(jsonParam)) {
			StringEntity entity = new StringEntity(jsonParam, ContentType.APPLICATION_JSON);
			post.setEntity(entity);
		}
		
		CloseableHttpResponse resp= httpclient.execute(post);
		String resultStr = EntityUtils.toString(resp.getEntity(), ENCODING);
		System.out.println("------>HttpClient POST RESULT DATA:" + resultStr);
		return resultStr;
	}

	/**
	 * HttpClient测试
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		Map<String, String> heads = new HashMap<>(16);
		heads.put("dataType", "json");
		heads.put("contentType", "application/json;charset=UTF-8");

		String postUrl = "http://127.0.0.1:8081/xxxx/xxxx/xxx/xxx.do";
		String idcardId = "";
		String paramJson = "{\"idcard\":\"" + idcardId + "\",\"state\":\"0\"}";
		String resStr = HttpClientUtil.postFun(postUrl, heads, paramJson);
		System.out.println("----------->" + resStr);
	}
}

 

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值