HttpClien使用介绍
在实际开发过程中,我们经常需要调用对方提供的接口(如回告上游接口)或第三方接口(短信、天气等),借鉴网上资料结合自己的理解,整理出来的代码,总结记录一下。
一.简介
HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。
官网地址: http://hc.apache.org/downloads.cgi
二、主要特征
- 基于标准、纯净的java语言。实现了Http1.0和Http1.1。
- 以可扩展的面向对象的结构实现了Http全部的方法(GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE)。
- 支持HTTPS协议。
- 通过Http代理建立透明的连接。
- 利用CONNECT方法通过Http代理建立隧道的https连接。
- Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO/Kerberos认证方案。
- 插件式的自定义认证方案。
- 便携可靠的套接字工厂使它更容易的使用第三方解决方案。
- 连接管理器支持多线程应用。支持设置最大连接数,同时支持设置每个主机的最大连接数,发现并关闭过期的连接。
- 自动处理Set-Cookie中的Cookie。
- 插件式的自定义Cookie策略。
- Request的输出流可以避免流中内容直接缓冲到socket服务器。
- Response的输入流可以有效的从socket服务器直接读取相应内容。
- 在http1.0和http1.1中利用KeepAlive保持持久连接。
- 直接获取服务器发送的response code和 headers。
- 设置连接超时的能力。
- 实验性的支持http1.1 response caching。
- 源代码基于Apache License 可免费获取。
三、使用步骤
1.引入json、guava、httpClient的相关Maven的依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
2.一般使用步骤
HttpGet请求响应流程
- 创建
httpClient
对象,HttpClientBuilder.create().build()
- 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建
HttpGet
对象;如果是带参数的GET的请求,则可以先使用URIBuilder(String url)
创建对象,再调用addParameters(nameValuePairs)
方法来设置请求参数。 - 创建CloseableHttpResponse,调用HttpClient对象的
execute(HttpUriRequest request,HttpContext context)
发送请求,该方法返回一个HttpResponse
。调用HttpResponse的getAllHeaders()
、getHeaders(String name)
等方法可获取服务器的响应头;调用HttpResponse的getStatusLine().getStatusCode()
可以获取响应状态码;调用HttpResponse的getEntity()
方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。 - 释放连接。无论执行方法是否成功,都必须释放连接
HttpPost请求响应流程与GET流程类似,这里不在介绍…
代码实例
package com.example.http.api;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
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.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
/**
* 作者: wangzai
* 时间: 2021/12/5 0:47
* 描述:
*/
@Slf4j
public class HttpUtils {
private static HttpClientContext httpClientContext = HttpClientContext.create();
/**
* get 带参数
* @param url
* @param requestParam
* @return
* @throws Exception
*/
public static HttpResp get(String url, Map<String,Object> requestParam)throws Exception{
//初始化httpClient
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse response = null;
try {
List<NameValuePair> nameValuePairs = _getNameValuePair(requestParam);
//拼装url参数
URI uri = new URIBuilder(url).addParameters(nameValuePairs).build();
// 声明 httpGet
HttpGet httpGet = new HttpGet();
httpGet.setURI(uri);
httpGet.setHeader("ContentType","application/json");
//配置连接信息
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(5000) //连接超时时间
.setSocketTimeout(5000) //响应超时时间
.build();
httpGet.setConfig(config);
// 发送请求
response = httpClient.execute(httpGet, httpClientContext);
HttpResp resp = _getHttpResponseInfo(response);
log.info("HttpUtils#get execute success url ={}, params={},",url, JSON.toJSONString(requestParam));
return resp;
}finally {
try {
if(null != response){
response.close();
}
}catch (Exception e){
log.error("HttpUtils#get execute error,url={}, params = {}",url, JSON.toJSONString(requestParam));
}
}
}
/**
* get 不带参数
* @param url
* @return
* @throws Exception
*/
public static HttpResp get(String url) throws Exception{
return get(url,null);
}
/**
* post 带请求参数
* @param url
* @param requestBody
* @return
* @throws Exception
*/
public static HttpResp post(String url,Object requestBody)throws Exception{
//初始化httpClient
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse response = null;
try {
// 声明 httpPost
HttpPost post = new HttpPost();
post.setHeader("Content-Type", "application/json");
//设置请求参数
post.setEntity(new StringEntity(JSON.toJSONString(requestBody), Charset.defaultCharset()));
//配置连接信息
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(5000)
.setSocketTimeout(5000)
.build();
post.setConfig(config);
// 发送请求
response = httpClient.execute(post, httpClientContext);
HttpResp httpResp = _getHttpResponseInfo(response);
log.info("HttpUtils#post execute success url ={}, params={},",url, JSON.toJSONString(requestBody));
return httpResp;
}finally {
try {
if(null != response){
response.close();
}
}catch (Exception e){
log.error("HttpUtils#post execute error,url={}, params = {}",url, JSON.toJSONString(requestBody),e);
}
}
}
/**
* 请求参数处理
* @param requestParams
* @return
* @throws IOException
*/
private static List<NameValuePair> _getNameValuePair(Map<String,Object> requestParams) throws IOException {
List<NameValuePair> nameValuePairs = Lists.newArrayList();
if(null != requestParams){
for (String key : requestParams.keySet()) {
String values = requestParams.get(key).toString();
nameValuePairs.add(new BasicNameValuePair(key,values));
}
}
return nameValuePairs;
}
/**
* HttpResponse 获取返回的 response信息
* @param response
* @return
* @throws Exception
*/
private static HttpResp _getHttpResponseInfo(CloseableHttpResponse response) throws Exception{
//如果状态码200,返回响应体信息,否则返回null
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode == HttpStatus.SC_OK){
return new HttpResp(statusCode,EntityUtils.toString(response.getEntity(), Charset.defaultCharset()));
}
return null;
}
}
自定义返回结果类型
@Data
@AllArgsConstructor
public class HttpResp {
/**
* 状态码
*/
private int code;
/**
* 响应体信息
*/
private String data;
}
测试实例
@GetMapping("/httpclient")
public String httpClient(){
try {
HttpResp resp = HttpUtils.get("https://baidu.com");
System.out.println(resp);
return "success";
}catch (Exception e){
log.error("HttpClientController#httpClient execute error",e.getMessage());
return "error";
}
}
个人学习总结,如写的有不足的地方,可在评论区指出
关于HttpClient连接池管理以及springBoot整合HttpClient的具体使用,参考
https://blog.csdn.net/wsywb111/article/details/80311716