文章目录
一、httpclient简介
- 官网:http://hc.apache.org/ 使用版本:4.5.13
- 使用场景
- 爬虫
- 多系统之间接口交互
二、jdk原生api发送http请求
HttpURLConnection 【Get】
package com.baosight.iplat4j.test.httpclient;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
public class JdkHttpClientTest {
@Test
public void test01() throws IOException {
String urlStr = "https://www.baidu.com/";
URL url = new URL(urlStr);
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
/*
设置请求类型
请求头
*/
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("Accept-Charset","utf-8");
//获取httpURLConnection 的输入流
try(InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);
) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
}
HttpURLConnection 【实例工作中使用】
/**
* 以raw形式发送post请求
* @param url 访问地址
* @param param 需要传输json参数(可以通过json工具转换成String)
* @param header header 参数
* @return 返回网页返回的数据
*/
public static String doPostRaw(String url, String param, Map<String, String> header) {
OutputStreamWriter out;
BufferedReader in;
StringBuilder result = new StringBuilder();
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
// 设置超时时间
conn.setConnectTimeout(5000);
conn.setReadTimeout(15000);
// 设置通用的请求属性
conn.setRequestMethod("POST");
conn.addRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Accept-Encoding", "gzip,deflate,br");
conn.setRequestProperty("Connection", "Keep-Alive");
// 如有特殊属性
if (header != null) {
for (Map.Entry<String, String> entry : header.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
}
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8);// utf-8编码
// 发送请求参数
out.write(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
out.close();
in.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
}
三、发送get请求
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
3.1 get 无参、无请求头
/**
* 使用Httpclient发送get请求
* 1. 无参请求
* @throws IOException
*/
@Test
public void test01() throws IOException {
/*
1. 【创建一个客户端对象】可关闭的httpclient客户端,相当于你打开的一个浏览器
*/
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
/*
2. 【创建请求方式对象】
*/
// 构造httpGet请求对象
String urlStr = "https://www.baidu.com/";
HttpGet httpGet = new HttpGet(urlStr);
// 响应
CloseableHttpResponse response = null;
try {
/*
3. 【发起请求】
*/
response = closeableHttpClient.execute(httpGet);
/*
4. 【对请求结果进行解析】
*/
/**
* HttpEntity 不仅可以作为结果,也可以作为请求的参数实体,有很多的实现
*/
// 获取响应结果
HttpEntity entity = response.getEntity();
// 对HttpEntity操作的工具类,将响应结果转为字符串
String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
System.out.println(toStringResult);
/*
5. 【释放资源】
*/
//确保流关闭
EntityUtils.consume(entity);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (closeableHttpClient != null) {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.2 get 无参、有请求头
/**
* 使用Httpclient发送get请求
* 3.2 无参有请求头
* @throws IOException
*/
@Test
public void test02() {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
/*
2. 【创建请求方式对象】
*/
String urlStr = "https://www.baidu.com/";
HttpGet httpGet = new HttpGet(urlStr);
/*
添加请求头
*/
//解决httpclient被认为不是真人行为
httpGet.addHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36");
// 防盗链,value:发生防盗链的网站的url
// httpGet.addHeader("Referer","");
CloseableHttpResponse response = null;
try {
response = closeableHttpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
System.out.println(toStringResult);
EntityUtils.consume(entity);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (closeableHttpClient != null) {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.3 有参,有请求头
/**
* 使用Httpclient发送get请求
* 3.3. 有参的请求需要使用urlencode编码
*
* @throws IOException
*/
@Test
public void test03() throws UnsupportedEncodingException {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
/*
2. 【创建请求方式对象】
*/
//get请求中请求参数需要使用URLencoder进行编码转换,浏览器会自动编码,web容器会自动解码,这里使用客户端发送时需要手动编码
String strParam = "123 + 134asdf | adsfads";//123+%2B+134asdf+%7C+adsfads
String encode = URLEncoder.encode(strParam, StandardCharsets.UTF_8.name()); // 使用jdk的编码器
String urlStr = "https://api.apiopen.top/api/getTime" + "&userName=" + encode;
// 构造httpGet请求对象
HttpGet httpGet = new HttpGet(urlStr);
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36");
CloseableHttpResponse response = null;
try {
response = closeableHttpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
System.out.println(toStringResult);
EntityUtils.consume(entity);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (closeableHttpClient != null) {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.4 获取响应头信息
/**
* 使用Httpclient发送get请求
* 3.4. 获取响应头以及相应的Content-Type
*
* @throws IOException
*/
@Test
public void test04() throws UnsupportedEncodingException {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
String strParam = "123 + 134asdf | adsfads";//123+%2B+134asdf+%7C+adsfads
String encode = URLEncoder.encode(strParam, StandardCharsets.UTF_8.name());
String urlStr = "https://api.apiopen.top/api/getTime" + "&userName=" + encode;
HttpGet httpGet = new HttpGet(urlStr);
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36");
CloseableHttpResponse response = null;
try {
response = closeableHttpClient.execute(httpGet);
/*
4. 【对请求结果进行解析】
*/
//代表本次请求的成功、失败的状态,响应状态码
StatusLine statusLine = response.getStatusLine();//响应状态码
if (HttpStatus.SC_OK == statusLine.getStatusCode()) {
System.out.println("响应成功");
// 获取所有响应头
Header[] allHeaders = response.getAllHeaders();
for (Header header : allHeaders) {
System.out.println("响应头" + header.getName() + "的值" + header.getValue());
}
HttpEntity entity = response.getEntity();
String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
// 获取单个响应头
System.out.println("ContentType:" + entity.getContentType());
System.out.println(toStringResult);
EntityUtils.consume(entity);
} else {
System.out.println("响应失败,响应码:" + statusLine.getStatusCode());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (closeableHttpClient != null) {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
类似的在请求对象中也有同样的操作获取请求头信息
3.5 保存网络图片到本地
/**
* 使用Httpclient发送get请求
* 3.5. 保存网络图片到本地
*
* @throws IOException
*/
@Test
public void test05() {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
String urlStr = "https://i2.hdslb.com/bfs/face/bc66c12ec774395bd7be8d60e6a664d6127ec8fa.jpg@240w_240h_1c_1s.webp";
HttpGet httpGet = new HttpGet(urlStr);
CloseableHttpResponse response = null;
try {
response = closeableHttpClient.execute(httpGet);
/*
4. 【对请求结果进行解析】
*/
HttpEntity entity = response.getEntity();
//从响应头中获取文件类型
// image/jpg image/jpeg image/png image/图片的后缀
String contentType = entity.getContentType().getValue();
String suffix = ".jpg";
if (contentType.contains("jpg") || contentType.contains("jpeg")) {
suffix = ".jpg";
} else if (contentType.contains("bmp") || contentType.contains("bitmap")) {
suffix = ".bmp";
} else if (contentType.contains("png")) {
suffix = ".png";
} else if (contentType.contains("gif")) {
suffix = ".gif";
}
// 获取文件的字节流
byte[] bytes = EntityUtils.toByteArray(entity);
// 创建文件输出流,将字节放入流中输出到文件
String localAbsPath = "/Users/Shared/work/pc/jhwz/SRC/src/test/java/com/baosight/iplat4j/test/httpclient/abc" + suffix;
FileOutputStream fos = new FileOutputStream(localAbsPath);
fos.write(bytes);
fos.close();
EntityUtils.consume(entity);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (closeableHttpClient != null) {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.6 设置访问代理
http://www.66ip.cn/
/**
* 使用Httpclient发送get请求
* 3.6. 设置访问代理
*
* @throws IOException
*/
@Test
public void test06() {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
/*
2. 【创建请求方式对象】
*/
String urlStr = "https://www.baidu.com/";
HttpGet httpGet = new HttpGet(urlStr);
// 创建一个代理
String ip = "103.168.44.137";
int port = 3127;
HttpHost proxy = new HttpHost(ip,port);
//对每一个请求进行配置,会覆盖全局的默认请求配置
RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).build();
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = null;
try {
response = closeableHttpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
System.out.println(toStringResult);
EntityUtils.consume(entity);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (closeableHttpClient != null) {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.7 设置连接超时时间、读取超时时间
/**
* 使用Httpclient发送get请求
* 3.7. 设置访问代理
*
* @throws IOException
*/
@Test
public void test07() {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
/*
2. 【创建请求方式对象】
*/
String urlStr = "https://www.baidu.com/";
HttpGet httpGet = new HttpGet(urlStr);
RequestConfig requestConfig = RequestConfig.custom()
//连接超时,ms,完成tcp3次握手的时间上限
.setSocketTimeout(5000)
//读取超时,ms,表示请求的网址处获取响应数据的时间间隔
.setSocketTimeout(3000)
//指从连接池里获取connection的超时时间间隔
.setConnectionRequestTimeout(5000)
.build();
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = null;
try {
response = closeableHttpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
System.out.println(toStringResult);
EntityUtils.consume(entity);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (closeableHttpClient != null) {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四、发送post请求
-
MIME type https://www.w3school.com.cn/media/media_mimeref.asp
-
含义:Multipurpose Internet Mail Extensions,即多用途互联网邮件扩展类型
-
组成
信息头 含义 举例 MIME-Version MIME版本 1.0 Content-Type 内容类型 application/x-www-form-urlencoded , application/json Content-Transfer-Encoding 编码格式 8bit, binary Content-Disposition 内容排列方式 上传文件时
Content-Disposition:form-data;name=“fileName”;filename=“C:\Users\lenovo\Desktop\a.html”
下载文件时需要设置:
Content-Disposition:attachment;filename=URLEncoder.encode(“xx.zip”,“UTF-8”) -
png—服务器上的web容器(Tomcat)通过后缀在conf/web.xml文件中找到对应的mime-mapping —> 设置响应头Content-Type的值为mime-type的值
-
网页form表单enctype 可用的MIME类型(Content-Type类型)
- application/x-www-form-urlencoded
- Multipart/form-data
- Text/plain
-
4.1 application/x-www-form-urlencoded
/**
* application/x-www-form-urlencoded
*/
@Test
public void test01() {
/*
1. 【创建一个客户端对象】可关闭的httpclient客户端,相当于你打开的一个浏览器
*/
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
/*
2. 【创建请求方式对象】
*/
// 构造httpPost请求对象
String urlStr = "https://api.apiopen.top/api/login";
HttpPost httpPost = new HttpPost(urlStr);
// 设置请求头
//不设置Content-Type 默认就是 application/x-www-form-urlencoded
httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
//设置请求参数
ArrayList<NameValuePair> list = new ArrayList<>();
list.add(new BasicNameValuePair("account","zs202051@163.com"));
list.add(new BasicNameValuePair("password", "123456"));
//把参数集合设置到formEntity
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list,Consts.UTF_8);
httpPost.setEntity(formEntity);
// 响应
CloseableHttpResponse response = null;
try {
/*
3. 【发起请求】
*/
response = closeableHttpClient.execute(httpPost);
/*
4. 【对请求结果进行解析】
*/
HttpEntity entity = response.getEntity();
String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
System.out.println(toStringResult);
/*
5. 【释放资源】
*/
EntityUtils.consume(entity);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (closeableHttpClient != null) {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.2 Application/json
@Test
public void test02() {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
String urlStr = "https://api.apiopen.top/api/login";
HttpPost httpPost = new HttpPost(urlStr);
//创建json字符串参数
String jsonStr =
"{\n" +
" \"account\": \"309324904@qq.com\",\n" +
" \"password\": \"123456\"\n" +
"}";
//设置请求参数
StringEntity jsonEntity = new StringEntity(jsonStr,Consts.UTF_8);
//也可以在这里设置请求内容类型,或在请求头中设置
//jsonEntity.setContentType("application/json");
jsonEntity.setContentType(new BasicHeader("Content-Type","application/json; charset=utf-8"));
//设置entity编码
jsonEntity.setContentEncoding(Consts.UTF_8.name());
httpPost.setEntity(jsonEntity);
CloseableHttpResponse response = null;
try {
response = closeableHttpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
System.out.println(toStringResult);
EntityUtils.consume(entity);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (closeableHttpClient != null) {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.3 上传文件
/**
* 演示 Content-Disposition
*/
@PostMapping(value="/test3")
@ResponseBody
public String test3(@RequestParam("fileName") MultipartFile[] multipartFiles,
String userName, String password, HttpServletRequest request, HttpServletResponse response){
for (MultipartFile multipartFile : multipartFiles) {
System.out.println("上传的文件名:" + multipartFile.getOriginalFilename());
}
System.out.println("其他业务入参:" + userName + " " + password);
//获取所有请求头
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headName = headerNames.nextElement();
System.out.println(headName + ":" + request.getHeader(headName));
}
return "test03";
}
<!--httpclient文件上传时会用到-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.13</version>
</dependency>
@Test
public void test03() {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
String urlStr = "http://localhost:8080/test3";
HttpPost httpPost = new HttpPost(urlStr);
// 构造上传文件使用的entity
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(Consts.UTF_8); // 设置编码
builder.setContentType(ContentType.create("multipart/form-data",Consts.UTF_8));
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); //设置浏览器模式
// 构建一个filebody对象
FileBody fileBody = new FileBody(new File("/Users/Shared/work/pc/jhwz/SRC/src/test/java/com/baosight/iplat4j/test/httpclient/JdkHttpClientTest.java"));
StringBody userNameTextBody = new StringBody("小明",ContentType.create("text/plain",Consts.UTF_8));
HttpEntity httpEntity = builder
.addPart("fileName",fileBody)
.addBinaryBody("fileName",new File("/Users/Shared/work/pc/jhwz/SRC/src/test/java/com/baosight/iplat4j/test/httpclient/abc.jpg"))
//对于普通的表单字段如果含有中文的话,不能通过addTextBody,否则乱码
//text:是指的输入的值
.addPart("userName",userNameTextBody)
.addTextBody("password","admin")
.build();
httpPost.setEntity(httpEntity);
CloseableHttpResponse response = null;
try {
response = closeableHttpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
System.out.println(toStringResult);
EntityUtils.consume(entity);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (closeableHttpClient != null) {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
五、请求https连接
- 安全的
- 不安全的
- 通过认证需要的秘钥配置httpclient
- 通过配置httpclient绕过https安全认证
/**
* 使用Httpclient发送get请求
* 1. 绕过安全协议
*
* @throws IOException
*/
@Test
public void test08() throws Exception {
/*
1. 【创建一个客户端对象】可关闭的httpclient客户端,相当于你打开的一个浏览器
*/
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", trustHttpsCertificates())
.build();
//创建一个ConnectionManager
PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager(registry);
//定制closeableHttpClient
HttpClientBuilder httpClientBuilder = HttpClients.custom().setConnectionManager(pool);
//配置好后通过build获取对象
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
String urlStr = "https://www.baidu.com/";
HttpGet httpGet = new HttpGet(urlStr);
CloseableHttpResponse response = null;
try {
response = closeableHttpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
System.out.println(toStringResult);
EntityUtils.consume(entity);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (closeableHttpClient != null) {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 创建支持安全协议的连接工厂
private ConnectionSocketFactory trustHttpsCertificates() throws Exception {
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
sslContextBuilder.loadTrustMaterial(null, new TrustStrategy() {
// 判断是否信任url
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
});
SSLContext sslContext = sslContextBuilder.build();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE);
return sslConnectionSocketFactory;
}
六、使用httpclient连接池
- 性能
- 连接的复用
七、封装通用的HttpClientUtil
package com.baosight.iplat4j.test.httpclient;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.*;
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.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
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.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.pool.PoolStats;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Slf4j
public class HttpClientUtil {
private static CloseableHttpClient closeableHttpClient;
private static PoolingHttpClientConnectionManager cm;
static {
HttpClientBuilder httpClientBuilder = HttpClients.custom();
/*
一、绕过不安全的https请求的证书验证
*/
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", trustHttpsCertificates())
.build();
/*
二、创建连接池管理对象
*/
cm = new PoolingHttpClientConnectionManager(registry);
cm.setMaxTotal(50); // 连接池最大有50个连接,<=20
/*
roadjava.com域名/ip+port 就是一个路由,
http://www.roadjava.com/s/spsb/beanvalidation/
http://www.roadjava.com/s/1.html
https://www.baidu.com/一个域名,又是一个新的路由
*/
cm.setDefaultMaxPerRoute(50); // 每个路由默认有多少连接,<=2
httpClientBuilder.setConnectionManager(cm);
/*
三、设置请求默认配置
*/
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000)
.setSocketTimeout(3000)
.setConnectionRequestTimeout(5000)
.build();
httpClientBuilder.setDefaultRequestConfig(requestConfig);
/*
四、设置默认的一些header
*/
List<Header> defaultHeaders = new ArrayList<>();
BasicHeader userAgentHeader = new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36");
defaultHeaders.add(userAgentHeader);
httpClientBuilder.setDefaultHeaders(defaultHeaders);
// 线程安全,此处初始化一次即可,通过上面的配置来生成一个用于管理多个连接的连接池closeableHttpClient
closeableHttpClient = httpClientBuilder.build();
}
/**
* 构造安全连接工厂
* @return SSLConnectionSocketFactory
*/
private static ConnectionSocketFactory trustHttpsCertificates() {
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
try {
sslContextBuilder.loadTrustMaterial(null, new TrustStrategy() {
// 判断是否信任url
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
});
SSLContext sslContext = sslContextBuilder.build();
return new SSLConnectionSocketFactory(sslContext,
new String[]{"SSLv2Hello","SSLv3","TLSv1","TLSv1.1","TLSv1.2"}
,null, NoopHostnameVerifier.INSTANCE);
} catch (Exception e) {
log.error("构造安全连接工厂失败",e);
throw new RuntimeException("构造安全连接工厂失败");
}
}
/**
* 发送get请求
* @param url 请求url,参数需经过URLEncode编码处理
* @param headers 自定义请求头
* @return 返回结果
*/
public static String executeGet(String url, Map<String,String> headers) {
// 构造httpGet请求对象
HttpGet httpGet = new HttpGet(url);
// 自定义请求头设置
if (headers != null) {
Set<Map.Entry<String, String>> entries = headers.entrySet();
for (Map.Entry<String, String> entry : entries) {
httpGet.addHeader(new BasicHeader(entry.getKey(),entry.getValue()));
}
}
// 可关闭的响应
CloseableHttpResponse response = null;
try {
log.info("prepare to execute url:{}",url);
response = closeableHttpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
if (HttpStatus.SC_OK == statusLine.getStatusCode()) {
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, StandardCharsets.UTF_8);
}else {
log.error("响应失败,响应码:"+statusLine.getStatusCode());
}
}catch (Exception e) {
log.error("executeGet error,url:{}",url,e);
} finally {
consumeRes(response);
}
return null;
}
/**
* 发送表单类型的post请求
* @param url 要请求的url
* @param list 参数列表
* @param headers 自定义头
* @return 返回结果
*/
public static String postForm(String url, List<NameValuePair> list, Map<String,String> headers) {
HttpPost httpPost = new HttpPost(url);
if (headers != null) {
Set<Map.Entry<String, String>> entries = headers.entrySet();
for (Map.Entry<String, String> entry : entries) {
httpPost.addHeader(new BasicHeader(entry.getKey(),entry.getValue()));
}
}
// 确保请求头一定是form类型
httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
// 给post对象设置参数
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list,Consts.UTF_8);
httpPost.setEntity(formEntity);
// 响应处理
CloseableHttpResponse response = null;
try {
log.info("prepare to execute url:{}",httpPost.getRequestLine());
response = closeableHttpClient.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
if (HttpStatus.SC_OK == statusLine.getStatusCode()) {
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, StandardCharsets.UTF_8);
}else {
log.error("响应失败,响应码:"+statusLine.getStatusCode());
}
}catch (Exception e) {
log.error("executeGet error,url:{}",url,e);
} finally {
consumeRes(response);
}
return null;
}
/**
* 发送json类型的post请求
* @param url 请求url
* @param body json字符串
* @param headers 自定义header
* @return 返回结果
*/
public static String postJson(String url, String body, Map<String,String> headers) {
HttpPost httpPost = new HttpPost(url);
// 设置请求头
if (headers != null) {
Set<Map.Entry<String, String>> entries = headers.entrySet();
for (Map.Entry<String, String> entry : entries) {
httpPost.addHeader(new BasicHeader(entry.getKey(),entry.getValue()));
}
}
// 确保请求头是json类型
httpPost.addHeader("Content-Type","application/json; charset=utf-8");
/*
设置请求体
*/
StringEntity jsonEntity = new StringEntity(body, Consts.UTF_8);
jsonEntity.setContentType("application/json; charset=utf-8");
jsonEntity.setContentEncoding(Consts.UTF_8.name());
httpPost.setEntity(jsonEntity);
CloseableHttpResponse response = null;
try {
response = closeableHttpClient.execute(httpPost);
printStat();
StatusLine statusLine = response.getStatusLine();
if (HttpStatus.SC_OK == statusLine.getStatusCode()) {
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, StandardCharsets.UTF_8);
}else {
log.error("响应失败,响应码:"+statusLine.getStatusCode());
}
}catch (Exception e) {
log.error("executeGet error,url:{}",url,e);
} finally {
consumeRes(response);
}
return null;
}
private static void printStat() {
// 连接池的最大连接数 50
// log.info("cm.getMaxTotal():{}",cm.getMaxTotal());
// 每一个路由的最大连接数 50
// log.info("cm.getDefaultMaxPerRoute():{}",cm.getDefaultMaxPerRoute());
PoolStats totalStats = cm.getTotalStats();
// 连接池的最大连接数 50
// log.info("totalStats.getMax():{}",totalStats.getMax());
// 连接池里面有多少连接是被占用了
log.info("totalStats.getLeased():{}",totalStats.getLeased());
// 连接池里面有多少连接是可用的
log.info("totalStats.getAvailable():{}",totalStats.getAvailable());
}
private static void consumeRes(CloseableHttpResponse response) {
// response.close();是关闭连接,不是归还连接到连接池
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
log.error("consume出错",e);
}
}
}
}
使用
public class TestHttpClientUtil {
@Test
public void test01() throws Exception {
String jsonStr = "[{\"purcMatSpecifi\":\"DL8100\",\"purcMatCode\":\"1000000895\",\"purcNum\":2.00,\"purcUnitPriceNoTax\":140.00,\"purcType2\":\"公开采购\",\"remark\":\" \",\"purcPlanName\":\"流程3\",\"purcMatName\":\"钢直尺\",\"purcProjectType\":\"非框架项目\",\"orgUnitCode\":\"010110\",\"otherSpecPara\":\"规格1000mm\",\"reqDatetime\":\"20220527\",\"purcAmount\":580.00,\"purcPlanNo\":\"PP2022072941\",\"purcUnit\":\"0077\",\"buyerUserId\":\"300340\",\"purcPrferBrand\":\" \",\"purcPlanType\":\"年度\",\"recId\":\"20220729000131\"},{\"purcMatSpecifi\":\"UT511\",\"purcMatCode\":\"1000000974\",\"purcNum\":2.00,\"purcUnitPriceNoTax\":150.00,\"purcType2\":\"公开采购\",\"remark\":\" \",\"purcPlanName\":\"流程3\",\"purcMatName\":\"兆欧表\",\"purcProjectType\":\"非框架项目\",\"orgUnitCode\":\"010110\",\"otherSpecPara\":\"输出电压:1000V 短路电流:<2mA 交流电压750V 直流电压:1000V (参考优利德品牌UT511型号)\",\"reqDatetime\":\"20220527\",\"purcAmount\":580.00,\"purcPlanNo\":\"PP2022072941\",\"purcUnit\":\"0072\",\"buyerUserId\":\"300340\",\"purcPrferBrand\":\" \",\"purcPlanType\":\"年度\",\"recId\":\"20220729000132\"}]";
String signature = getSignature("2e1135be-0747-4e8e-afad-163ae4bcb953","c271ff70-22d6-11ed-9076-0775c2b0b3af","23e2d5e7-6d0d-4a7a-9862-fafb5fea1f9f");
Map<String, String> headerMap = new HashMap<>();
headerMap.put("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
headerMap.put("Accept","*/*");
headerMap.put("Accept-Encoding","gzip,deflate,br");
headerMap.put("Connection","Keep-Alive");
headerMap.put("appid", "2e1135be-0747-4e8e-afad-163ae4bcb953");
headerMap.put("apiname", "c271ff70-22d6-11ed-9076-0775c2b0b3af");
headerMap.put("signature", signature.replace("\n", "").replace("\r", ""));
String result = HttpClientUtil.postJson("http://10.200.92.7/clientgateway/", jsonStr, headerMap);
System.out.println(result);
}
/**
* 获得总线接口需要的验证参数
* @param appid
* @param apiname
* @param appkey
* @return
*/
public static String getSignature(String appid,String apiname,String appkey) throws Exception{
String stringtosign = appid+ apiname + new Date().getTime();
String key = appkey.replace("-", ""); // key 要去“-”
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes("utf-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] bytes = cipher.doFinal(stringtosign.getBytes("utf-8"));
String signature = new BASE64Encoder().encode(bytes);
return signature;
}
}
八、Hutool工具httpclient使用
发送 application/json
类型的请求
@Test
public void test02() throws Exception {
String jsonStr = "[{\"purcMatSpecifi\":\"DL8100\",\"purcMatCode\":\"1000000895\",\"purcNum\":2.00,\"purcUnitPriceNoTax\":140.00,\"purcType2\":\"公开采购\",\"remark\":\" \",\"purcPlanName\":\"流程3\",\"purcMatName\":\"钢直尺\",\"purcProjectType\":\"非框架项目\",\"orgUnitCode\":\"010110\",\"otherSpecPara\":\"规格1000mm\",\"reqDatetime\":\"20220527\",\"purcAmount\":580.00,\"purcPlanNo\":\"PP2022072941\",\"purcUnit\":\"0077\",\"buyerUserId\":\"300340\",\"purcPrferBrand\":\" \",\"purcPlanType\":\"年度\",\"recId\":\"20220729000131\"},{\"purcMatSpecifi\":\"UT511\",\"purcMatCode\":\"1000000974\",\"purcNum\":2.00,\"purcUnitPriceNoTax\":150.00,\"purcType2\":\"公开采购\",\"remark\":\" \",\"purcPlanName\":\"流程3\",\"purcMatName\":\"兆欧表\",\"purcProjectType\":\"非框架项目\",\"orgUnitCode\":\"010110\",\"otherSpecPara\":\"输出电压:1000V 短路电流:<2mA 交流电压750V 直流电压:1000V (参考优利德品牌UT511型号)\",\"reqDatetime\":\"20220527\",\"purcAmount\":580.00,\"purcPlanNo\":\"PP2022072941\",\"purcUnit\":\"0072\",\"buyerUserId\":\"300340\",\"purcPrferBrand\":\" \",\"purcPlanType\":\"年度\",\"recId\":\"20220729000132\"}]";
Map<String, String> headerMap = new HashMap<>();
headerMap.put("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
headerMap.put("Accept","*/*");
headerMap.put("Accept-Encoding","gzip,deflate,br");
headerMap.put("Connection","Keep-Alive");
headerMap.put("appid", "xxxxxxxx");
headerMap.put("apiname", "xxxxxxx");
headerMap.put("signature", signature.replace("\n", "").replace("\r", ""));
String connection = HttpRequest.post("http://xxxxxx/clientgateway/")
.contentType("application/json")
.addHeaders(headerMap)
.body(jsonStr).execute().body();
System.out.println(connection);
}
发送 application/x-www-form-urlencoded
类型的请求
//获取code的值,用于接口中查询使用
HashMap map = new HashMap();
String code = inInfo.getString("code");
Preconditions.checkArgument(StringUtils.isNotBlank(code),"code参数不能为空");
Map<String, String> headerMap = new HashMap<>();
headerMap.put("isToken", "false");
headerMap.put("Authorization", "Basic xxxxxxxxxxxx");
HashMap<String, Object> m = new HashMap();
m.put("grant_type", "authorization_code");
m.put("code", code);
m.put("redirect_uri","http://localhost:8080/");
//发送http请求 'http://10.200.92.16/sso/auth/oauth/token'
String connection = HttpRequest.post("http://xxxxxxx/sso/auth/oauth/token")
.contentType("application/x-www-form-urlencoded")
.addHeaders(headerMap)
.form(m).execute().body();
System.out.println(connection);