一、简介
1、HttpClient
HttpClient 是Apache HttpComponents 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient 是Apache HttpComponents 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
2、HTTP协议的特点如下
- 支持客户/服务器模式。
- 简单快速:客户向服务器请求服务时,只需传送请求方法和路径。请求方法常用的有GET、HEAD、POST。每种方法规定了客户与服务器联系的类型不同。 由于HTTP协议简单,使得HTTP服务器的程序规模小,因而通信速度很快。
- 灵活:HTTP允许传输任意类型的数据对象。正在传输的类型由Content-Type加以标记。
- 无连接:无连接的含义是限制每次连接只处理一个请求。服务器处理完客户的请求,并收到客户的应答后,即断开连接。采用这种方式可以节省传输时间。
- 无状态:HTTP协议是无状态协议。无状态是指协议对于事务处理没有记忆能力。缺少状态意味着如果后续处理需要前面的信息,则它必须重传,这样可能导致每次连接传送的数据量增大。另一方面,在服务器不需要先前信息时它的应答就较快。
3、使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可
- 创建HttpClient对象。
- 创建请求方式的实例。创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
- 添加请求参数。如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
- 发送Http请求。调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
- 获取返回的内容。调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
- 释放资源。无论执行方法是否成功,都必须释放资源;
二、spring boot集成HttpClient
1、pom.xml添加httpclient的jar包依赖
代码语言:javascript复制<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
2、测试类
代码语言:javascript复制import com.lydms.utils.HttpClientUtils;
import java.io.IOException;
import java.util.HashMap;
public class test {
public static void main(String[] args) {
test test = new test();
//测试get请求
test.testGet();
//测试String类型Post请求
test.testStringPost();
//测试Map类型Post请求
test.testMapPost();
}
/**
* 测试POST请求(String入参)
*/
private void testStringPost() {
String url = "http://107.12.57.187:8080/sms/post1";
String str = "{"english":"json"}";
try {
String result = HttpClientUtils.post(url, str);
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 测试POST请求(Map入参)
*/
private void testMapPost() {
String url = "http://107.12.57.187:8080/sms/post1";
HashMap<String, String> map = new HashMap<>();
map.put("english", "json");
try {
String result = HttpClientUtils.post(url, map);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 测试GET请求
*/
private void testGet() {
String url = "http://107.12.57.187:8080/sms/get";
try {
String result = HttpClientUtils.get(url);
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、工具类
代码语言:javascript复制import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
public class HttpClientUtils {
private static final Logger logger = LogManager.getLogger(HttpClientUtils.class);
/**
* 封装POST请求(Map入参)
*
* @param url 请求的路径
* @param map 请求的参数
* @return
* @throws IOException
*/
public static String post(String url, Map map) throws IOException {
// 1、创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 2、创建请求方式的实例
HttpPost httpPost = new HttpPost();
try {
httpPost.setURI(new URI(url));
} catch (URISyntaxException e) {
e.printStackTrace();
}
// 3、添加请求参数(设置请求和传输超时时间)
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();
httpPost.setConfig(requestConfig);
ArrayList<NameValuePair> list = new ArrayList<>();
Set<Map.Entry> entrySet = map.entrySet();
for (Map.Entry entry : entrySet) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
list.add(new BasicNameValuePair(key, value));
}
httpPost.setEntity(new UrlEncodedFormEntity(list, org.apache.http.protocol.HTTP.UTF_8));
// 4、发送Http请求
HttpResponse response = httpClient.execute(httpPost);
// 5、获取返回的内容
String result = null;
int statusCode = response.getStatusLine().getStatusCode();
if (200 == statusCode) {
result = EntityUtils.toString(response.getEntity());
} else {
logger.info("请求第三方接口出现错误,状态码为:{}", statusCode);
return null;
}
// 6、释放资源
httpPost.abort();
httpClient.getConnectionManager().shutdown();
return result;
}
/**
* 封装POST请求(String入参)
*
* @param url 请求的路径
* @param data String类型数据
* @return
* @throws IOException
*/
public static String post(String url, String data) throws IOException {
// 1、创建HttpClient对象
HttpClient httpClient = HttpClientBuilder.create().build();
// 2、创建请求方式的实例
HttpPost httpPost = new HttpPost(url);
// 3、添加请求参数(设置请求和传输超时时间)
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
// 设置请求参数
httpPost.setEntity(new StringEntity(data, "UTF-8"));
// 4、发送Http请求
HttpResponse response = httpClient.execute(httpPost);
// 5、获取返回的内容
String result = null;
int statusCode = response.getStatusLine().getStatusCode();
if (200 == statusCode) {
result = EntityUtils.toString(response.getEntity());
} else {
logger.info("请求第三方接口出现错误,状态码为:{}", statusCode);
return null;
}
// 6、释放资源
httpPost.abort();
httpClient.getConnectionManager().shutdown();
return result;
}
/**
* 封装GET请求
*
* @param url
* @return
* @throws IOException
*/
public static String get(String url) throws IOException {
// 1、创建HttpClient对象
HttpClient httpClient = HttpClientBuilder.create().build();
// 2、创建请求方式的实例
HttpGet httpGet = new HttpGet(url);
// 3、添加请求参数(设置请求和传输超时时间)
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();
httpGet.setConfig(requestConfig);
// 4、发送Http请求
HttpResponse response = httpClient.execute(httpGet);
// 5、获取返回的内容
String result = null;
int statusCode = response.getStatusLine().getStatusCode();
if (200 == statusCode) {
result = EntityUtils.toString(response.getEntity());
} else {
logger.info("请求第三方接口出现错误,状态码为:{}", statusCode);
return null;
}
// 6、释放资源
httpGet.abort();
httpClient.getConnectionManager().shutdown();
return result;
}
}
四、HttpUtils工具类
1、application.properties
代码语言:javascript复制#最大连接数
http.maxTotal=100
#设置到某个路由的最大连接数
http.defaultMaxPerRoute=20
#连接超时时间(单位毫秒)
http.connectTimeout=10000
#从连接池中获取到连接的最长时间(单位毫秒)
http.connectionRequestTimeout=5000
#数据传输的最长时间(单位毫秒)
http.socketTimeout=10000
#空闲永久连接检查间隔,官方推荐使用这个来检查永久链接的可用性,而不推荐每次请求的时候才去检查
http.validateAfterInactivity=2000
旧版本配置文件
代码语言:javascript复制#提交请求前测试连接是否可用(旧版_已不在使用)
http.staleConnectionCheckEnabled=true
#依赖common 版本4.0 http.validateAfterInactivity 替换 http.staleConnectionCheckEnabled
2、HttpClientconfig
代码语言:javascript复制import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HttpClientConfig {
@Value("${http.maxTotal}")
private Integer maxTotal;
@Value("${http.defaultMaxPerRoute}")
private Integer defaultMaxPerRoute;
@Value("${http.connectTimeout}")
private Integer connectTimeout;
@Value("${http.connectionRequestTimeout}")
private Integer connectionRequestTimeout;
@Value("${http.socketTimeout}")
private Integer socketTimeout;
@Value("${http.validateAfterInactivity}")
private Integer validateAfterInactivity;
@Bean
public CloseableHttpClient getCloseableHttpClient() {
PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager();
// 最大连接数
httpClientConnectionManager.setMaxTotal(maxTotal);
// 设置到某个路由的最大连接数
httpClientConnectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
// 空闲永久连接检查间隔,官方推荐使用这个来检查永久链接的可用性,而不推荐每次请求的时候才去检查
httpClientConnectionManager.setValidateAfterInactivity(validateAfterInactivity);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setConnectionManager(httpClientConnectionManager);
return httpClientBuilder.build();
}
@Bean
public RequestConfig getRequestConfig() {
RequestConfig.Builder builder = RequestConfig.custom();
// 连接超时时间(单位毫秒)
builder.setConnectTimeout(connectTimeout)
// 从连接池中获取到连接的最长时间(单位毫秒)
.setConnectionRequestTimeout(connectionRequestTimeout)
// 数据传输的最长时间(单位毫秒)
.setSocketTimeout(socketTimeout);
return builder.build();
}
}
3、HttpUtils
代码语言:javascript复制package com.test.testhive.Utils;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
public class HttpUtils {
private static final Logger logger = LogManager.getLogger(HttpUtils.class);
@Autowired
private CloseableHttpClient httpClient;
@Autowired
private RequestConfig config;
/**
* 发送POST请求
*
* @param url 请求URL
* @param param 请求体
* @return
*/
public String doPost(String url, Map<String, Object> param) {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(config);
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (Map.Entry<String, Object> entry : param.entrySet()) {
paramList.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
return EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
logger.error("调用第三方错误:{}", e.getMessage());
return null;
} finally {
try {
httpPost.abort();
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 发送post请求
*
* @param url 请求URL
* @param param 请求体
* @param headers 请求头信息
* @return
*/
public String doPost(String url, Map<String, Object> param, Map<String, String> headers) {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(config);
if (!CollectionUtils.isEmpty(headers)) {
headers.forEach((k, v) -> httpPost.addHeader(k, v));
}
CloseableHttpResponse response = null;
try {
// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (Map.Entry<String, Object> entry : param.entrySet()) {
paramList.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
return EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
logger.error("调用第三方错误:{}", e.getMessage());
return null;
} finally {
try {
httpPost.abort();
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}