【HttpUtil】基于 HttpClient 的简易 Java 工具类

一、Maven依赖项

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.13</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.13</version>
</dependency>

二、源代码

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;

import java.io.IOException;
import java.util.Map;

/**
 * @author NXY666
 */
public class HttpUtil {
	private static Header[] parseHeaders(Map<String, String> headers) {
		Header[] headersArray = new Header[headers.size()];
		int i = 0;
		for (Map.Entry<String, String> entry : headers.entrySet()) {
			headersArray[i] = new BasicHeader(entry.getKey(), entry.getValue());
			i++;
		}
		return headersArray;
	}

	private static String parseParams(Map<String, String> params) {
		if (params == null || params.isEmpty()) {
			return null;
		}
		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, String> entry : params.entrySet()) {
			sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
		}
		return sb.toString();
	}

	/**
	 * 发送Get请求
	 *
	 * @param url     请求地址
	 * @param params  请求参数
	 * @param headers 请求头
	 * @return 响应结果,若请求失败则返回null。
	 */
	public static CloseableHttpResponse get(String url, Map<String, String> params, Map<String, String> headers) throws IOException {
		CloseableHttpClient httpClient;

		//通过默认配置创建一个httpClient实例
		httpClient = HttpClients.createDefault();

		//创建httpGet远程连接实例
		String param = parseParams(params);
		if (param != null) {
			url = url + "?" + param;
		}
		HttpGet httpGet = new HttpGet(url);

		httpGet.setHeaders(parseHeaders(headers));

		//配置请求参数
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30 * 1000)
			.setConnectionRequestTimeout(30 * 1000)
			.setSocketTimeout(60 * 1000)
			.build();
		httpGet.setConfig(requestConfig);

		//执行get请求得到返回对象
		return httpClient.execute(httpGet);
	}

	/**
	 * 发送Post请求
	 *
	 * @param url     请求地址
	 * @param params  请求参数
	 * @param headers 请求头
	 * @param entity  请求实体
	 * @return 响应结果
	 */
	public static CloseableHttpResponse post(String url, Map<String, String> params, Map<String, String> headers, HttpEntity entity) throws IOException {
		//创建httpClient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();

		//创建http请求
		String param = parseParams(params);
		if (param != null) {
			url = url + "?" + param;
		}
		HttpPost httpPost = new HttpPost(url);

		//设置请求头
		httpPost.setHeaders(parseHeaders(headers));

		//配置请求参数
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30 * 1000)
			.setConnectionRequestTimeout(30 * 1000)
			.setSocketTimeout(60 * 1000)
			.build();
		httpPost.setConfig(requestConfig);

		//设置请求内容
		httpPost.setEntity(entity);

		//执行get请求得到返回对象
		return httpClient.execute(httpPost);
	}

	/**
	 * 发送Post请求
	 *
	 * @param url     请求地址
	 * @param params  请求参数
	 * @param headers 请求头
	 * @param data    请求数据
	 * @return 响应结果
	 */
	public static CloseableHttpResponse post(String url, Map<String, String> params, Map<String, String> headers, String data) throws IOException {
		return post(url, params, headers, new StringEntity(data));
	}

	/**
	 * 发送Multipart请求
	 *
	 * @param url     请求地址
	 * @param params  请求参数
	 * @param headers 请求头
	 * @param data    请求数据
	 * @return 响应结果
	 */
	public static CloseableHttpResponse multipart(String url, Map<String, String> params, Map<String, String> headers, Map<String, ContentBody> data) throws IOException {
		MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
		for (Map.Entry<String, ContentBody> dataPair : data.entrySet()) {
			multipartEntityBuilder.addPart(dataPair.getKey(), dataPair.getValue());
		}
		multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		HttpEntity reqEntity = multipartEntityBuilder.build();

		return post(url, params, headers, reqEntity);
	}
}

三、备注

  1. 这是一个非常简易的Http请求工具类,很多功能已被写死忽略,如有需要可根据需求自行添加。
  2. 部分代码有借鉴网络上的其它Http工具类。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GreatNXY

阿巴阿巴阿巴阿巴

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值