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;
}
}
HttpClientUtil
最新推荐文章于 2020-08-04 15:55:42 发布
本文介绍了一个用于发送HTTP请求的Java工具类,包括GET和POST请求的实现,支持超时设置和重试机制,同时展示了如何处理响应结果并记录调用耗时。
2010

被折叠的 条评论
为什么被折叠?



