cn.net.wanmo.common.send.HttpJdkSender Maven / Gradle / Ivy
The newest version!
package cn.net.wanmo.common.send;
import cn.net.wanmo.common.http.jdk.HttpJdkUtil;
import cn.net.wanmo.common.http.jdk.pojo.ResData;
import cn.net.wanmo.common.result.HttpResult;
import cn.net.wanmo.common.util.DateUtil;
import cn.net.wanmo.common.util.MapUtil;
import cn.net.wanmo.common.util.ObjectUtil;
import cn.net.wanmo.common.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.Objects;
public class HttpJdkSender {
private static final Logger logger = LoggerFactory.getLogger(HttpJdkSender.class);
public static ResObj get(String title, String url, ReqObj req, ResObj res) {
logger.debug(title + "接口URL:{}", url);
logger.debug(title + "接口请求头:{}", req.getHeaders());
HttpJdkUtil httpJdkUtil;
if (req.isUrlEncode == null) { // 是否需要 url 编码
httpJdkUtil = HttpJdkUtil.get(url).header(req.getHeaders());
} else {
httpJdkUtil = HttpJdkUtil.get(url, req.getUrlEncode()).header(req.getHeaders());
}
Map params = req.getParams();
if (MapUtil.isNotEmpty(params)) {
for (Map.Entry entry : params.entrySet()) {
httpJdkUtil.form(entry.getKey(), entry.getValue());
}
}
HttpResult httpResult = httpJdkUtil.send(req.isDownload());
return exec(title, httpResult, req, res);
}
public static ResObj post(String title, String url, ReqObj req, ResObj res) {
logger.debug(title + " 接口URL:{}", url);
logger.debug(title + " 接口请求头:{}", req.getHeaders());
logger.debug(title + " 接口请求参数:{}", req.getParams());
logger.debug(title + " 接口请求体:{}", req.getBody());
HttpJdkUtil httpJdkUtil;
if (req.isUrlEncode == null) { // 是否需要 url 编码
httpJdkUtil = HttpJdkUtil.post(url).header(req.getHeaders());
} else {
httpJdkUtil = HttpJdkUtil.post(url, req.getUrlEncode()).header(req.getHeaders());
}
Map params = req.getParams();
if (MapUtil.isNotEmpty(params)) {
for (Map.Entry entry : params.entrySet()) {
httpJdkUtil.form(entry.getKey(), entry.getValue());
}
}
BaseReq.ReqFile file = req.getFile();
if (Objects.nonNull(file)) {
httpJdkUtil.form(file.getName(), file.getFile(), file.getFilename());
}
if (StringUtil.isNotBlank(req.getBody())) {
httpJdkUtil.body(req.getBody());
}
HttpResult httpResult = httpJdkUtil.send(req.isDownload());
return exec(title, httpResult, req, res);
}
private static ResObj exec(String title, HttpResult httpResult, ReqObj req, ResObj res) {
try {
res.setErrCode(ObjectUtil.cast(httpResult.getCode()));
res.setErrMsg(httpResult.getMsg());
res.setSuccessHttp(httpResult.isSuccess());
if (httpResult.isSuccess()) {
ResData data = httpResult.getData();
logger.debug(title + " 接口响应头:{}", data.getHeaders());
logger.debug(title + " 接口响应体:{}", data.getBody());
res.setHeaders(data.getHeaders());
res.setBody(data.getBody());
if (res.isSuccessData()) { // 接口响应成功数据
logger.debug(title + " 接口成功:{}", res.toJSONString());
} else { // 接口响应失败数据
logger.warn(title + " 接口失败:{}", res.toJSONString());
}
} else {
logger.warn(title + " 接口HTTP失败:{} {}", res.getErrCode(), res.getErrMsg());
}
} catch (Exception e) {
res.setErrCode(ObjectUtil.cast(500));
res.setErrMsg(title + " 接口HTTP异常");
logger.error(res.getErrMsg(), e);
} finally {
res.setResTime(DateUtil.nowLong()); // 设置响应时间
res.setConsumeTime(res.getResTime() - req.getReqTime()); // 计算耗时
}
return res;
}
}