HttpClientUtil

package com.ykt.ffan.common.http;


import java.util.Date;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ykt.ffan.common.utils.StringUtil;

import lombok.extern.slf4j.Slf4j;



/**
 * @author: lvyong6
 */
@Slf4j
public class HttpClientUtil {
    private static String charset = "UTF-8";

    public static String doGet(String path, Map<String, String> params) {
        return doCall(path, params, HttpMethod.GET);
    }

    public static String doPost(String path, Map<String, String> params, int httpClinetSoTimeout) {
       if(httpClinetSoTimeout>0) {
          params.put("httpClinetSoTimeout", httpClinetSoTimeout+"");
       }
        return doCall(path, params, HttpMethod.POST);
    }

    private static String doCall(String path, Map<String, String> params, HttpMethod method) {
        if (StringUtil.isBlank(path)) {
            throw new IllegalArgumentException("Parameter 'path' is empty.");
        }

        Date startTime = new Date();
        // 构造request对象
        Request request = new Request();
        request.setGateway(path);
        request.setMethod(method);
        request.setCharset(charset);
        String httpClinetSoTimeout = params.get("httpClinetSoTimeout");
        
        if(null !=httpClinetSoTimeout) {
           request.setSoTimeout(Integer.parseInt(httpClinetSoTimeout));
        }
        if (params != null && !params.isEmpty()) {
            for (String key : params.keySet()) {
                request.addParam(key, params.get(key));
            }
        }
        Response response = HttpCaller.getInstance().call(request, null);
        Date endTime = new Date();
        if (response.isSuccess()) {
            response.setRespCharset(charset);
            String result = response.getStringResult();
            // 将结果输出到日志, 超出500字符进行截取
            StringBuffer sbf = new StringBuffer();
            if (result.length() > 500) {
                sbf.append(result.substring(0, 500));
                sbf.append("..omit..");
            } else {
                sbf.append(result);
            }
            long costtime = endTime.getTime() - startTime.getTime();
            log.info(String.format("调用服务 : %s; response text: %s\t costtime(ms) : %d", path, sbf.toString(),
                    costtime));
            return result;
        }

        return null;
    }

    public static String doGet(String path, Map<String, String> params, int connectionTimeout, int soTimeOut) {
        return doCall(path, params, HttpMethod.GET, connectionTimeout, soTimeOut, null);
    }

    public static String doPost(String path, Map<String, String> params, int connectionTimeout, int soTimeOut) {
        return doCall(path, params, HttpMethod.POST, connectionTimeout, soTimeOut, null);
    }

    public static String doGet(String path, Map<String, String> params, int connectionTimeout, int soTimeOut,
                               Integer retryCount) {
        return doCall(path, params, HttpMethod.GET, connectionTimeout, soTimeOut, retryCount);
    }

    public static String doPost(String path, Map<String, String> params, int connectionTimeout, int soTimeOut,
                                Integer retryCount) {
        return doCall(path, params, HttpMethod.POST, connectionTimeout, soTimeOut, retryCount);
    }

    private static String doCall(String path, Map<String, String> params, HttpMethod method, int connectionTimeout,
                                 int soTimeOut, Integer retryCount) {
        if (StringUtil.isBlank(path)) {
            throw new IllegalArgumentException("Parameter 'path' is empty.");
        }
        Date startTime = new Date();
        // 构造request对象
        Request request = new Request();
        request.setGateway(path);
        request.setMethod(method);
        request.setCharset(charset);
        request.setConnectionTimeout(connectionTimeout);
        request.setSoTimeout(soTimeOut);
        if (params != null && !params.isEmpty()) {
            for (String key : params.keySet()) {
                request.addParam(key, params.get(key));
            }
        }
        Response response = HttpCaller.getInstance().call(request, retryCount);
        Date endTime = new Date();
        if (response.isSuccess()) {
            response.setRespCharset(charset);
            String result = response.getStringResult();
            // 将结果输出到日志, 超出500字符进行截取
            StringBuffer sbf = new StringBuffer();
            if (result.length() > 500) {
                sbf.append(result.substring(0, 500));
                sbf.append("..omit..");
            } else {
                sbf.append(result);
            }
            long costtime = endTime.getTime() - startTime.getTime();
            log.info(String.format("调用服务 : %s; response text: %s\t costtime(ms) : %d", path, sbf.toString(),
                    costtime));
            return result;
        }

        return null;
    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值