import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
public class HttpClientUtil {
private static final Logger log = Logger.getLogger("lavasoft");
private static final String CHARSET_UTF_8 = "UTF-8";
private static final String CONTENT_TYPE_JSON = "application/json";
private static final String CONTENT_TYPE_XML = "text/xml";
private static PoolingHttpClientConnectionManager connMgr;
private static RequestConfig requestConfig;
private static final int MAX_TIMEOUT = 7000;
static {
connMgr = new PoolingHttpClientConnectionManager();
connMgr.setMaxTotal(100);
connMgr.setDefaultMaxPerRoute(connMgr.getMaxTotal());
connMgr.setValidateAfterInactivity(1000);
RequestConfig.Builder configBuilder = RequestConfig.custom();
configBuilder.setConnectTimeout(MAX_TIMEOUT);
configBuilder.setSocketTimeout(MAX_TIMEOUT);
configBuilder.setConnectionRequestTimeout(MAX_TIMEOUT);
requestConfig = configBuilder.build();
}
public static String get(String url) {
return doGet(url, new HashMap<>());
}
public static String getHttps(String url) {
return doGet(url, new HashMap<>(), false);
}
public static String getHttps(String url, Map<String, Object> params) {
return doGet(url, params, false);
}
public static String doGet(String url, Map<String, Object> params, boolean bl) {
log.info("____请求参数:" + params.toString());
String param = getStringBuffer(params);
String finalApiUrl = url + param;
return HttpClient.domain(httpClient -> {
HttpGet httpGet = new HttpGet(finalApiUrl);
return execute(httpClient, httpGet);
}, bl);
}
public static String doGet(String url, Map<String, Object> params) {
return doGet(url, params, true);
}
private static String getStringBuffer(Map<String, Object> params) {
if (MapUtil.isNotNull(params)) {
StringBuffer param = new StringBuffer();
int i = 0;
for (String key : params.keySet()) {
if (i == 0) {
param.append("?");
} else {
param.append("&");
}
param.append(key).append("=").append(params.get(key));
i++;
}
return param.toString();
} else {
return "";
}
}
public static String post(String url, Map<String, Object> params) {
log.info("____请求参数:" + params.toString());
return HttpClient.domain(httpClient -> {
HttpPost httpPost = httpPostHandler(url, params);
return execute(httpClient, httpPost);
}, true);
}
public static String post(String url) {
return post(url, new HashMap<>());
}
public static String postJson(String url, String jsonStr) {
log.info("____请求参数:" + jsonStr);
return HttpClient.domain(httpClient -> {
HttpPost httpPost = new HttpPost(url);
StringEntity stringEntity;
try {
stringEntity = new StringEntity(jsonStr);
} catch (UnsupportedEncodingException e) {
return null;
}
httpPost.setHeader("Content-Type", CONTENT_TYPE_JSON);
httpPost.setEntity(stringEntity);
return execute(httpClient, httpPost);
}, true);
}
public static String doPostSSL(String url, Map<String, Object> params) {
log.info("____请求参数:" + params.toString());
return HttpClient.domain(httpClient -> {
HttpPost httpPost = httpPostHandler(url, params);
return execute(httpClient, httpPost);
}, false);
}
public static String doPostSSL(String apiUrl, Object json) {
log.info("____请求参数:" + json);
return HttpClient.domain(httpClient -> {
HttpPost httpPost = new HttpPost(apiUrl);
StringEntity stringEntity;
stringEntity = new StringEntity(json.toString(), CHARSET_UTF_8);
stringEntity.setContentEncoding(CHARSET_UTF_8);
httpPost.setHeader("Content-Type", CONTENT_TYPE_JSON);
httpPost.setEntity(stringEntity);
return execute(httpClient, httpPost);
}, false);
}
private static HttpPost httpPostHandler(String url, Map<String, Object> params) {
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> naps = new ArrayList<>();
if (MapUtil.isNotNull(params)) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
naps.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
}
try {
httpPost.setEntity(new UrlEncodedFormEntity(naps, CHARSET_UTF_8));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return httpPost;
}
private static String execute(CloseableHttpClient httpClient, HttpRequestBase httpGetOrPost) {
String res = null;
CloseableHttpResponse response = null;
try {
httpGetOrPost.setConfig(requestConfig);
log.info("________请求url:" + httpGetOrPost.getURI());
response = httpClient.execute(httpGetOrPost);
log.info("________返回code:" + response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
return null;
}
HttpEntity entity = response.getEntity();
res = EntityUtils.toString(entity, CHARSET_UTF_8);
log.info("________返回值:" + res);
} catch (IOException e) {
e.printStackTrace();
} finally {
doResponseClose(response);
}
return res;
}
private static void doHttpClientClose(CloseableHttpClient httpClient) {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void doResponseClose(CloseableHttpResponse response) {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public interface HttpClientInterface<T> {
T domain(CloseableHttpClient httpClient);
}
static class HttpClient {
public static <T extends Object> T domain(HttpClientInterface<T> interfaces, boolean bl) {
T object;
CloseableHttpClient httpClient;
if (bl) {
httpClient = HttpClients.createDefault();
} else {
httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).
setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).
setConnectionManagerShared(true).build();
}
try {
object = interfaces.domain(httpClient);
} finally {
doHttpClientClose(httpClient);
}
return object;
}
}
private static SSLConnectionSocketFactory createSSLConnSocketFactory() {
SSLConnectionSocketFactory sslsf = null;
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (chain, authType) -> true).build();
sslsf = new SSLConnectionSocketFactory(sslContext, (arg0, agr1) -> true);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
return sslsf;
}