All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.net.wanmo.common.send.HttpApacheSender Maven / Gradle / Ivy

There is a newer version: 1.3.9
Show newest version
package cn.net.wanmo.common.send;

import cn.net.wanmo.common.http.apache.HttpApacheResult;
import cn.net.wanmo.common.http.apache.HttpApacheUtil;
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 HttpApacheSender {
    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());

        HttpApacheResult apacheResult = HttpApacheUtil.get(url)
                .header(req.getHeaders())
                .execute(req.isDownload());

        return exec(title, apacheResult, 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());

        HttpApacheUtil apacheUtil = HttpApacheUtil.post(url)
                .header(req.getHeaders());

        Map params = req.getParams();
        if (MapUtil.isNotEmpty(params)) {
            for (Map.Entry entry : params.entrySet()) {
                apacheUtil.form(entry.getKey(), entry.getValue());
            }
        }

        BaseReq.ReqFile reqFile = req.getFile();
        if (Objects.nonNull(reqFile)) {
            apacheUtil.form(reqFile.getName(), reqFile.getFile(), reqFile.getFilename());
        }

        if (StringUtil.isNotBlank(req.getBody())) {
            apacheUtil.body(req.getBody());
        }

        HttpApacheResult apacheResult = apacheUtil.execute(req.isDownload());
        return exec(title, apacheResult, req, res);
    }

    private static  ResObj exec(String title, HttpApacheResult httpResult, ReqObj req, ResObj res) {
        try {
            res.setErrCode(ObjectUtil.cast(httpResult.getStatusCode()));
            res.setErrMsg(httpResult.getMessage());
            res.setSuccessHttp(httpResult.isOk());

            if (httpResult.isOk()) {
                logger.debug(title + " 接口响应头:{}", httpResult.headers());
                logger.debug(title + " 接口响应体:{}", httpResult.body());

                res.setHeaders(httpResult.headers());
                res.setBody(httpResult.body());

                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;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy