package cn.huge.base.common.utils;
|
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.collections.CollectionUtils;
|
import org.apache.http.HttpEntity;
|
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.*;
|
import org.apache.http.entity.ContentType;
|
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.mime.HttpMultipartMode;
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
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 java.io.File;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.UnsupportedEncodingException;
|
import java.nio.charset.Charset;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
|
/**
|
* @title: HTTP请求工具类
|
* @description: HTTP请求工具类
|
* @company: hugeinfo
|
* @author: liyj
|
* @time: 2021-11-05 16:51:48
|
* @version: 1.0.0
|
*/
|
@Slf4j
|
public class HttpClientUtils {
|
|
/**
|
* 发送http get请求
|
* @param url 请求地址
|
* @param headers 请求头
|
* @param encode 编码
|
* @return
|
*/
|
public static String httpGet(String url, Map<String, String> headers, String encode){
|
if(encode == null){
|
encode = "utf-8";
|
}
|
String content = null;
|
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
|
HttpGet httpGet = new HttpGet(url);
|
//设置header
|
if (ObjectUtils.isNotEmpty(headers)) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
httpGet.setHeader(entry.getKey(),entry.getValue());
|
}
|
}
|
CloseableHttpResponse httpResponse = null;
|
try {
|
httpResponse = closeableHttpClient.execute(httpGet);
|
HttpEntity entity = httpResponse.getEntity();
|
content = EntityUtils.toString(entity, encode);
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}finally{
|
try {
|
httpResponse.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
try { //关闭连接、释放资源
|
closeableHttpClient.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return content;
|
}
|
|
/**
|
* 发送 http post 请求,参数以form表单键值对的形式提交。
|
* @param url 请求地址
|
* @param params 表单数据
|
* @param headers 请求头
|
* @param encode 编码
|
* @return
|
*/
|
public static String httpPostForm(String url, Map<String, String> params, Map<String, String> headers, String encode){
|
if(encode == null){
|
encode = "utf-8";
|
}
|
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
|
HttpPost httpost = new HttpPost(url);
|
//设置header
|
if (ObjectUtils.isNotEmpty(headers)) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
httpost.setHeader(entry.getKey(),entry.getValue());
|
}
|
}
|
//组织请求参数
|
List<NameValuePair> paramList = new ArrayList <NameValuePair>();
|
|
if(ObjectUtils.isNotEmpty(params)){
|
Set<String> keySet = params.keySet();
|
for(String key : keySet) {
|
paramList.add(new BasicNameValuePair(key, params.get(key)));
|
}
|
}
|
try {
|
httpost.setEntity(new UrlEncodedFormEntity(paramList, encode));
|
} catch (UnsupportedEncodingException e1) {
|
e1.printStackTrace();
|
}
|
String content = null;
|
CloseableHttpResponse httpResponse = null;
|
try {
|
httpResponse = closeableHttpClient.execute(httpost);
|
HttpEntity entity = httpResponse.getEntity();
|
content = EntityUtils.toString(entity, encode);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}finally{
|
try {
|
httpResponse.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
|
}
|
}
|
try { //关闭连接、释放资源
|
closeableHttpClient.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return content;
|
}
|
|
/**
|
* 发送 http post 请求,参数以原生字符串进行提交
|
* @param url 请求地址
|
* @param stringJson json字符串数据
|
* @param headers 请求头
|
* @param encode 编码
|
* @return
|
* @throws Exception
|
*/
|
public static String httpPostRaw(String url, String stringJson, Map<String, String> headers, String encode) throws Exception{
|
if(encode == null){
|
encode = "utf-8";
|
}
|
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
|
//设置请求和传输超时时间
|
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();
|
HttpPost httpost = new HttpPost(url);
|
httpost.setConfig(requestConfig);
|
//设置header
|
httpost.setHeader("Content-type", "application/json");
|
if (ObjectUtils.isNotEmpty(headers)) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
httpost.setHeader(entry.getKey(),entry.getValue());
|
}
|
}
|
//组织请求参数
|
StringEntity stringEntity = new StringEntity(stringJson, encode);
|
httpost.setEntity(stringEntity);
|
String content = null;
|
CloseableHttpResponse httpResponse = null;
|
try {
|
//响应信息
|
httpResponse = closeableHttpClient.execute(httpost);
|
HttpEntity entity = httpResponse.getEntity();
|
content = EntityUtils.toString(entity, encode);
|
} catch (Exception e) {
|
e.printStackTrace();
|
throw new Exception("网络连接异常,请稍候重试!", e);
|
}finally{
|
try {
|
httpResponse.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
try { //关闭连接、释放资源
|
closeableHttpClient.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
throw new Exception("网络连接异常,请稍候重试!", e);
|
}
|
return content;
|
}
|
|
/**
|
* 发送 http put 请求,参数以原生字符串进行提交
|
* @param url 请求地址
|
* @param stringJson json字符串数据
|
* @param headers 请求头
|
* @param encode 编码
|
* @return
|
*/
|
public static String httpPutRaw(String url, String stringJson, Map<String, String> headers, String encode){
|
if(encode == null){
|
encode = "utf-8";
|
}
|
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
|
HttpPut httpput = new HttpPut(url);
|
//设置header
|
httpput.setHeader("Content-type", "application/json");
|
if (ObjectUtils.isNotEmpty(headers)) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
httpput.setHeader(entry.getKey(),entry.getValue());
|
}
|
}
|
//组织请求参数
|
StringEntity stringEntity = new StringEntity(stringJson, encode);
|
httpput.setEntity(stringEntity);
|
String content = null;
|
CloseableHttpResponse httpResponse = null;
|
try {
|
//响应信息
|
httpResponse = closeableHttpClient.execute(httpput);
|
HttpEntity entity = httpResponse.getEntity();
|
content = EntityUtils.toString(entity, encode);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}finally{
|
try {
|
httpResponse.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
try {
|
closeableHttpClient.close(); //关闭连接、释放资源
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return content;
|
}
|
|
/**
|
* 发送http delete请求
|
* @param url 请求地址
|
* @param headers 请求头
|
* @param encode 编码
|
* @return
|
*/
|
public static String httpDelete(String url,Map<String, String> headers,String encode){
|
if(encode == null){
|
encode = "utf-8";
|
}
|
String content = null;
|
//since 4.3 不再使用 DefaultHttpClient
|
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
|
HttpDelete httpdelete = new HttpDelete(url);
|
//设置header
|
if (ObjectUtils.isNotEmpty(headers)) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
httpdelete.setHeader(entry.getKey(),entry.getValue());
|
}
|
}
|
CloseableHttpResponse httpResponse = null;
|
try {
|
httpResponse = closeableHttpClient.execute(httpdelete);
|
HttpEntity entity = httpResponse.getEntity();
|
content = EntityUtils.toString(entity, encode);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}finally{
|
try {
|
httpResponse.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
try { //关闭连接、释放资源
|
closeableHttpClient.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return content;
|
}
|
|
/**
|
* 发送 http post 附带单个个文件请求,参数以form表单键值对的形式提交。
|
* @param url 请求地址
|
* @param params 表单数据
|
* @param fileKey 文件参数名
|
* @param files File格式文件
|
* @param headers 请求头
|
* @param encode 编码
|
* @return
|
*/
|
public static String httpPostFormFile(String url, Map<String, String> params, String fileKey, File file, Map<String, String> headers,String encode){
|
if(encode == null){
|
encode = "utf-8";
|
}
|
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
|
HttpPost httpost = new HttpPost(url);
|
|
//设置header
|
if (ObjectUtils.isNotEmpty(headers)) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
httpost.setHeader(entry.getKey(),entry.getValue());
|
}
|
}
|
MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create();
|
mEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
|
mEntityBuilder.setCharset(Charset.forName(encode));
|
|
// 普通参数
|
ContentType contentType = ContentType.create("text/plain",Charset.forName(encode));
|
if (ObjectUtils.isNotEmpty(params)) {
|
Set<String> keySet = params.keySet();
|
for (String key : keySet) {
|
mEntityBuilder.addTextBody(key, params.get(key), contentType);
|
}
|
}
|
//二进制参数
|
mEntityBuilder.addBinaryBody(fileKey, file);
|
httpost.setEntity(mEntityBuilder.build());
|
String content = null;
|
CloseableHttpResponse httpResponse = null;
|
try {
|
httpResponse = closeableHttpClient.execute(httpost);
|
HttpEntity entity = httpResponse.getEntity();
|
content = EntityUtils.toString(entity, encode);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}finally{
|
try {
|
httpResponse.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
try { //关闭连接、释放资源
|
closeableHttpClient.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return content;
|
}
|
|
/**
|
* 发送 http post 附带多个文件请求,参数以form表单键值对的形式提交。
|
* @param url 请求地址
|
* @param params 表单数据
|
* @param fileKey 文件参数名
|
* @param files File格式文件
|
* @param headers 请求头
|
* @param encode 编码
|
* @return
|
*/
|
public static String httpPostFormFiles(String url, Map<String, String> params, String fileKey, List<File> files, Map<String, String> headers,String encode){
|
if(encode == null){
|
encode = "utf-8";
|
}
|
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
|
HttpPost httpost = new HttpPost(url);
|
|
//设置header
|
if (ObjectUtils.isNotEmpty(headers)) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
httpost.setHeader(entry.getKey(),entry.getValue());
|
}
|
}
|
MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create();
|
mEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
|
mEntityBuilder.setCharset(Charset.forName(encode));
|
|
// 普通参数
|
ContentType contentType = ContentType.create("text/plain",Charset.forName(encode));
|
if (ObjectUtils.isNotEmpty(params)) {
|
Set<String> keySet = params.keySet();
|
for (String key : keySet) {
|
mEntityBuilder.addTextBody(key, params.get(key), contentType);
|
}
|
}
|
//二进制参数
|
if (CollectionUtils.isNotEmpty(files)) {
|
for (File file : files) {
|
mEntityBuilder.addBinaryBody(fileKey, file);
|
}
|
}
|
httpost.setEntity(mEntityBuilder.build());
|
String content = null;
|
CloseableHttpResponse httpResponse = null;
|
try {
|
httpResponse = closeableHttpClient.execute(httpost);
|
HttpEntity entity = httpResponse.getEntity();
|
content = EntityUtils.toString(entity, encode);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}finally{
|
try {
|
httpResponse.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
try { //关闭连接、释放资源
|
closeableHttpClient.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return content;
|
}
|
|
/**
|
* 发送 http post 附带单个文件请求,参数以form表单键值对的形式提交。
|
*
|
* @param url 请求地址
|
* @param params 表单数据
|
* @param inputStreamKey 文件参数名
|
* @param inputStreams 文件流格式文件
|
* @param headers 请求头
|
* @param encode 编码
|
* @return
|
*/
|
public static String httpPostFormInputStream(String url, Map<String, String> params, String inputStreamKey, InputStream inputStream, Map<String, String> headers, String encode){
|
if(encode == null){
|
encode = "utf-8";
|
}
|
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
|
HttpPost httpost = new HttpPost(url);
|
|
//设置header
|
if (ObjectUtils.isNotEmpty(headers)) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
httpost.setHeader(entry.getKey(),entry.getValue());
|
}
|
}
|
MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create();
|
mEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
|
mEntityBuilder.setCharset(Charset.forName(encode));
|
|
// 普通参数, 解决中文乱码
|
ContentType contentType = ContentType.create("text/plain",Charset.forName(encode));
|
if (ObjectUtils.isNotEmpty(params)) {
|
Set<String> keySet = params.keySet();
|
for (String key : keySet) {
|
mEntityBuilder.addTextBody(key, params.get(key),contentType);
|
}
|
}
|
//文件流参数
|
mEntityBuilder.addBinaryBody(inputStreamKey, inputStream);
|
httpost.setEntity(mEntityBuilder.build());
|
String content = null;
|
CloseableHttpResponse httpResponse = null;
|
try {
|
httpResponse = closeableHttpClient.execute(httpost);
|
HttpEntity entity = httpResponse.getEntity();
|
content = EntityUtils.toString(entity, encode);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}finally{
|
try {
|
httpResponse.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
try { //关闭连接、释放资源
|
closeableHttpClient.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return content;
|
}
|
|
/**
|
* 发送 http post 附带多
|
* 个文件请求,参数以form表单键值对的形式提交。
|
*
|
* @param url 请求地址
|
* @param params 表单数据
|
* @param inputStreamKey 文件参数名
|
* @param inputStreams 文件流格式文件
|
* @param headers 请求头
|
* @param encode 编码
|
* @return
|
*/
|
public static String httpPostFormInputStreams(String url, Map<String, String> params, String inputStreamKey, List<InputStream> inputStreams, Map<String, String> headers, String encode){
|
if(encode == null){
|
encode = "utf-8";
|
}
|
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
|
HttpPost httpost = new HttpPost(url);
|
|
//设置header
|
if (ObjectUtils.isNotEmpty(headers)) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
httpost.setHeader(entry.getKey(),entry.getValue());
|
}
|
}
|
MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create();
|
mEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
|
mEntityBuilder.setCharset(Charset.forName(encode));
|
|
// 普通参数, 解决中文乱码
|
ContentType contentType = ContentType.create("text/plain",Charset.forName(encode));
|
if (ObjectUtils.isNotEmpty(params)) {
|
Set<String> keySet = params.keySet();
|
for (String key : keySet) {
|
mEntityBuilder.addTextBody(key, params.get(key),contentType);
|
}
|
}
|
//文件流参数
|
if (CollectionUtils.isNotEmpty(inputStreams)){
|
for (InputStream inputStream : inputStreams) {
|
mEntityBuilder.addBinaryBody(inputStreamKey, inputStream);
|
}
|
}
|
httpost.setEntity(mEntityBuilder.build());
|
String content = null;
|
CloseableHttpResponse httpResponse = null;
|
try {
|
httpResponse = closeableHttpClient.execute(httpost);
|
HttpEntity entity = httpResponse.getEntity();
|
content = EntityUtils.toString(entity, encode);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}finally{
|
try {
|
httpResponse.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
try { //关闭连接、释放资源
|
closeableHttpClient.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return content;
|
}
|
|
/**
|
* 发送http get请求,获取文件流
|
* @param url 请求地址
|
* @param headers 请求头
|
* @param encode 编码
|
* @return
|
*/
|
public static InputStream httpInputStreamGet(String url, Map<String, String> headers, String encode){
|
if(encode == null){
|
encode = "utf-8";
|
}
|
InputStream inputStream = null;
|
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
|
HttpGet httpGet = new HttpGet(url);
|
//设置header
|
if (ObjectUtils.isNotEmpty(headers)) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
httpGet.setHeader(entry.getKey(),entry.getValue());
|
}
|
}
|
CloseableHttpResponse httpResponse = null;
|
try {
|
httpResponse = closeableHttpClient.execute(httpGet);
|
HttpEntity entity = httpResponse.getEntity();
|
inputStream = entity.getContent();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}finally{
|
try {
|
httpResponse.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
try { //关闭连接、释放资源
|
closeableHttpClient.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return inputStream;
|
}
|
|
}
|
/**
|
* -------------------_ooOoo_-------------------
|
* ------------------o8888888o------------------
|
* ------------------88" . "88------------------
|
* ------------------(| -_- |)------------------
|
* ------------------O\ = /O------------------
|
* ---------------____/`---'\____---------------
|
* -------------.' \\| |// `.-------------
|
* ------------/ \\||| : |||// \------------
|
* -----------/ _||||| -:- |||||- \-----------
|
* -----------| | \\\ - /// | |-----------
|
* -----------| \_| ''\---/'' | |-----------
|
* -----------\ .-\__ `-` ___/-. /-----------
|
* ---------___`. .' /--.--\ `. . __----------
|
* ------."" '< `.___\_<|>_/___.' >'"".-------
|
* -----| | : `- \`.;`\ _ /`;.`/ - ` : | |-----
|
* -----\ \ `-. \_ __\ /__ _/ .-` / /-----
|
* ======`-.____`-.___\_____/___.-`____.-'======
|
* -------------------`=---='
|
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
* ---------佛祖保佑---hugeinfo---永无BUG----------
|
*/
|