http client工具类
@UtilityClass
@Slf4j
public class HttpUtils {
public static String doGet(String url, Map<String, String> headList, JSONObject params) {
HttpGet httpGet = null;
judgeTheUrl(url);
try {
httpGet = getHttpGet(params, url);
return getResponse(httpGet, headList, null);
} catch (Exception e) {
e.printStackTrace();
return "HTTP_ERROR" + e.toString();
} finally {
if (httpGet != null) {
httpGet.releaseConnection();
}
}
}
public static String doGet2(String url, Map<String, String> headList, JSONObject params) throws IOException {
HttpGet httpGet = null;
judgeTheUrl(url);
httpGet = getHttpGet(params, url);
return getResponse(httpGet, headList, null);
}
public static String doGetHasTimeLimit(String url, Map<String, String> headList,
JSONObject params, Integer timeout) throws IOException {
HttpGet httpGet = null;
judgeTheUrl(url);
httpGet = getHttpGet(params, url);
return getResponse(httpGet, headList, timeout);
}
public static String doSoapPost(String url, Map<String, String> headList, String params) {
HttpPost httpPost = null;
judgeTheUrl(url);
try {
httpPost = getHttpPost(params, url);
return getResponse(httpPost, headList, null);
} catch (Exception e) {
e.printStackTrace();
return "HTTP_ERROR" + e.toString();
} finally {
if (httpPost != null) {
httpPost.releaseConnection();
}
}
}
private static HttpPost getHttpPost(String params, String url) {
HttpPost httpPost;
if (StringUtils.isNoneBlank(params)) {
StringEntity stringEntity = new StringEntity(params, StandardCharsets.UTF_8);
httpPost = new HttpPost(url);
httpPost.setEntity(stringEntity);
} else {
httpPost = new HttpPost(url);
}
return httpPost;
}
public static HttpContext doGetInputStream(String url, Map<String, String> headList, JSONObject params) {
HttpGet httpGet;
judgeTheUrl(url);
try {
httpGet = getHttpGet(params, url);
return HttpContext.builder()
.inputStream(getInputStreamResponse(httpGet, headList))
.httpRequestBase(httpGet)
.build();
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
}
public static HttpContext doGetInputStream(String url, Map<String, String> headList, List<String> params) {
HttpGet httpGet;
judgeTheUrl(url);
try {
httpGet = getHttpGet(params, url);
HttpResponse httpResponse = getHttpResponse(httpGet, headList, null);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity httpEntity = httpResponse.getEntity();
return HttpContext.builder()
.inputStream(httpEntity.getContent())
.httpRequestBase(httpGet)
.responseHeaders(httpResponse.getHeaders("Content-disposition"))
.build();
} else {
log.error("获取响应异常");
}
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
}
public static String doPost(String url, Map<String, String> headList, JSONObject params) {
HttpPost httpPost = null;
judgeTheUrl(url);
try {
httpPost = getHttpPost(params, url, true);
return getResponse(httpPost, headList, null);
} catch (Exception e) {
e.printStackTrace();
return "HTTP_ERROR" + e.toString();
} finally {
if (httpPost != null) {
httpPost.releaseConnection();
}
}
}
public static String doFormDataPost(String url, Map<String, String> headList, JSONObject params) {
HttpPost httpPost = null;
judgeTheUrl(url);
try {
httpPost = getHttpPost(params, url, false);
return getResponse(httpPost, headList, null);
} catch (Exception e) {
e.printStackTrace();
return "HTTP_ERROR" + e.toString();
} finally {
if (httpPost != null) {
httpPost.releaseConnection();
}
}
}
public static HttpContext doPostInputStream(String url, Map<String, String> headList, JSONObject params) {
HttpPost httpPost;
judgeTheUrl(url);
try {
httpPost = getHttpPost(params, url, true);
return HttpContext.builder()
.inputStream(getInputStreamResponse(httpPost, headList))
.httpRequestBase(httpPost)
.build();
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
}
private static HttpPost getHttpPost(JSONObject params, String url, Boolean jsonContent) {
HttpPost httpPost;
if (params != null && params.size() > 0) {
if (jsonContent) {
StringEntity stringEntity = new StringEntity(params.toString(), StandardCharsets.UTF_8);
httpPost = new HttpPost(url);
httpPost.setEntity(stringEntity);
} else {
httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(getNameValuePair(params)));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
} else {
httpPost = new HttpPost(url);
}
return httpPost;
}
private static HttpGet getHttpGet(JSONObject params, String url) throws IOException {
HttpGet httpGet;
List<NameValuePair> paramStr = getNameValuePair(params);
if (paramStr != null && paramStr.size() > 0) {
String str = EntityUtils.toString(new UrlEncodedFormEntity(paramStr, "UTF-8"));
httpGet = new HttpGet(url + "?" + str);
} else {
httpGet = new HttpGet(url);
}
return httpGet;
}
private static HttpGet getHttpGet(List<String> params, String url) throws IOException {
HttpGet httpGet;
if (params != null && params.size() > 0) {
String paramStr = StringUtils.join(params, "/");
httpGet = new HttpGet(url + "/" + paramStr);
} else {
httpGet = new HttpGet(url);
}
return httpGet;
}
private static InputStream getInputStreamResponse(HttpRequestBase httpRequestBase, Map<String, String> headList) throws IOException {
HttpResponse httpResponse = getHttpResponse(httpRequestBase, headList, null);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity httpEntity = httpResponse.getEntity();
return httpEntity.getContent();
} else {
log.error("获取响应异常");
return null;
}
}
private static HttpResponse getHttpResponse(HttpRequestBase httpRequestBase, Map<String, String> headList, Integer timeout) throws IOException {
HttpClient httpClient = HttpClients.createDefault();
httpRequestBase.setConfig(getRequestConfig(timeout));
if (headList != null && headList.size() > 0) {
headList.forEach(httpRequestBase::addHeader);
}
return httpClient.execute(httpRequestBase);
}
private static String getResponse(HttpRequestBase httpRequestBase, Map<String, String> headList, Integer timeout) throws IOException {
HttpResponse httpResponse = getHttpResponse(httpRequestBase, headList, timeout);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toString(httpEntity);
}
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toString(httpEntity);
}
private static List<NameValuePair> getNameValuePair(JSONObject params) {
if (params == null) {
return null;
}
List<NameValuePair> nameValuePairs = new LinkedList<>();
params.forEach((k, v) -> nameValuePairs.add(new BasicNameValuePair(k, (String) v)));
return nameValuePairs;
}
private static RequestConfig getRequestConfig(Integer timeout) {
if (Objects.nonNull(timeout) && timeout <= 0) {
throw new RuntimeException("时间不可小于0");
}
return RequestConfig.custom()
.setConnectionRequestTimeout(Objects.isNull(timeout) ? 50000 : timeout)
.setConnectTimeout(Objects.isNull(timeout) ? 50000 : timeout)
.setSocketTimeout(Objects.isNull(timeout) ? 50000 : timeout).build();
}
private static void judgeTheUrl(String url) {
if (StringUtils.isBlank(url)) {
throw new BusinessException("发起请求的url不能为空,请输入");
}
}
@Data
@Builder
public class HttpContext {
private InputStream inputStream;
private HttpRequestBase httpRequestBase;
private Header[] responseHeaders;
}
}