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, Mapparams) { return doCall(path, params, HttpMethod.GET); } public static String doPost(String path, Map params, int httpClinetSoTimeout) { if(httpClinetSoTimeout>0) { params.put("httpClinetSoTimeout", httpClinetSoTimeout+""); } return doCall(path, params, HttpMethod.POST); } private static String doCall(String path, Map 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 params, int connectionTimeout, int soTimeOut) { return doCall(path, params, HttpMethod.GET, connectionTimeout, soTimeOut, null); } public static String doPost(String path, Map params, int connectionTimeout, int soTimeOut) { return doCall(path, params, HttpMethod.POST, connectionTimeout, soTimeOut, null); } public static String doGet(String path, Map params, int connectionTimeout, int soTimeOut, Integer retryCount) { return doCall(path, params, HttpMethod.GET, connectionTimeout, soTimeOut, retryCount); } public static String doPost(String path, Map params, int connectionTimeout, int soTimeOut, Integer retryCount) { return doCall(path, params, HttpMethod.POST, connectionTimeout, soTimeOut, retryCount); } private static String doCall(String path, Map 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; } }