|
|
|
package com.qs.serve.common.util;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.qs.serve.common.model.dto.R;
|
|
|
|
import lombok.experimental.UtilityClass;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
import org.apache.http.ParseException;
|
|
|
|
import org.apache.http.client.ClientProtocolException;
|
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
|
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.impl.client.CloseableHttpClient;
|
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
|
import org.apache.http.protocol.HTTP;
|
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author YenHex
|
|
|
|
* @since 2022/11/23
|
|
|
|
*/
|
|
|
|
@Slf4j
|
|
|
|
@UtilityClass
|
|
|
|
public class HttpUtil {
|
|
|
|
|
|
|
|
private final static String CONTENT_TYPE = "application/json";
|
|
|
|
private final static String CHARSET = "UTF-8";
|
|
|
|
private final static String EN_CONING = "UTF-8";
|
|
|
|
|
|
|
|
public static String doPost(String url, String jsonStr, HashMap<String,String> headers) {
|
|
|
|
CloseableHttpClient client = HttpClientBuilder.create().build();
|
|
|
|
CloseableHttpResponse response = null;
|
|
|
|
String errorMsg = null;
|
|
|
|
try {
|
|
|
|
//设post请求
|
|
|
|
HttpPost post = new HttpPost(url);
|
|
|
|
//创建请求实体传参
|
|
|
|
if(jsonStr!=null){
|
|
|
|
StringEntity postingString = new StringEntity(jsonStr,CHARSET);
|
|
|
|
postingString.setContentEncoding(EN_CONING);
|
|
|
|
//设置post请求参数
|
|
|
|
post.setEntity(postingString);
|
|
|
|
}
|
|
|
|
post.addHeader(HTTP.CONTENT_TYPE,CONTENT_TYPE);
|
|
|
|
if (headers != null) {
|
|
|
|
for (String key : headers.keySet()) {
|
|
|
|
post.addHeader(key,headers.get(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
post.setConfig(
|
|
|
|
RequestConfig.custom()
|
|
|
|
// 连接超时30秒,减少数据库连接占用时长
|
|
|
|
// 缩短时长,避免事务时间过长,占用数据库连接
|
|
|
|
.setConnectTimeout(30*1000)
|
|
|
|
.build()
|
|
|
|
);
|
|
|
|
//执行post请求
|
|
|
|
response = client.execute(post);
|
|
|
|
//访问成功状态码为200
|
|
|
|
if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
|
return EntityUtils.toString(response.getEntity());
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
errorMsg = e.getMessage();
|
|
|
|
e.printStackTrace();
|
|
|
|
}finally {
|
|
|
|
try {
|
|
|
|
client.close();
|
|
|
|
if(response!=null){
|
|
|
|
response.close();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errorMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String doGet(String url, HashMap<String,String> headers) {
|
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
|
try {
|
|
|
|
HttpGet httpget = new HttpGet(url);
|
|
|
|
httpget.addHeader(HTTP.CONTENT_TYPE,CONTENT_TYPE);
|
|
|
|
if (headers != null) {
|
|
|
|
for (String key : headers.keySet()) {
|
|
|
|
httpget.addHeader(key,headers.get(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CloseableHttpResponse response = httpclient.execute(httpget);
|
|
|
|
try {
|
|
|
|
if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
|
return EntityUtils.toString(response.getEntity());
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
response.close();
|
|
|
|
}
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
httpclient.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String createUrl(Map<String,Object> param){
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (String key : param.keySet()) {
|
|
|
|
String par = param.get(key).toString();
|
|
|
|
try {
|
|
|
|
par = URLEncoder.encode(par,"UTF-8");
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
sb.append("&").append(key).append("=").append(par);
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static R<String> doGetResult(String url,HashMap<String,String> headers){
|
|
|
|
String result = HttpUtil.doGet(url,headers);
|
|
|
|
log.debug("url:{},result:{}",url,result);
|
|
|
|
if(result==null){
|
|
|
|
Assert.throwEx("远程服务连接失败");
|
|
|
|
}
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
|
Integer status = jsonObject.getInteger("status");
|
|
|
|
String msg = jsonObject.getString("msg");
|
|
|
|
String data = jsonObject.getString("data");
|
|
|
|
return new R(status,msg,data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static R<String> doPostResult(String url, String jsonStr, HashMap<String,String> headers){
|
|
|
|
String result = HttpUtil.doPost(url,jsonStr,headers);
|
|
|
|
log.debug("url:{},result:{}",url,result);
|
|
|
|
if(result==null){
|
|
|
|
log.warn("jsonStr:{}",jsonStr);
|
|
|
|
Assert.throwEx("远程服务连接失败");
|
|
|
|
}
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
|
Integer status = jsonObject.getInteger("status");
|
|
|
|
String msg = jsonObject.getString("msg");
|
|
|
|
String data = jsonObject.getString("data");
|
|
|
|
return new R(status,msg,data);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|