|
@@ -0,0 +1,326 @@
|
|
|
+package utils;
|
|
|
+
|
|
|
+import constant.CommonConstant;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.HttpStatus;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
+import org.apache.http.client.methods.*;
|
|
|
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
|
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
+import org.apache.http.conn.ssl.TrustStrategy;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.message.BasicHttpResponse;
|
|
|
+import org.apache.http.message.BasicNameValuePair;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.security.KeyManagementException;
|
|
|
+import java.security.KeyStoreException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.cert.X509Certificate;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Map.Entry;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 处理简单http请求的类
|
|
|
+ *
|
|
|
+ * @author tangxiangan
|
|
|
+ */
|
|
|
+public class HttpClientUtils {
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientUtils.class);
|
|
|
+
|
|
|
+ private static CloseableHttpClient getHttpClient() {
|
|
|
+ TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
|
|
|
+
|
|
|
+ SSLContext sslContext;
|
|
|
+ try {
|
|
|
+ sslContext = org.apache.http.ssl.SSLContexts.custom()
|
|
|
+ .loadTrustMaterial(null, acceptingTrustStrategy)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
|
|
|
+
|
|
|
+ return HttpClients.custom()
|
|
|
+ .setSSLSocketFactory(csf)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
|
|
|
+ LOGGER.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+
|
|
|
+ LOGGER.warn("error create http client, create default client will not support https!!!!");
|
|
|
+ return HttpClients.createDefault();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 处理GET类请求,参数URL应包含完整的请求参数
|
|
|
+ public static String doGet(String url) {
|
|
|
+ return doGet(url, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String doGet(String url, Map<String, Object> params) {
|
|
|
+ return doGet(url, params, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String doGet(String url, Map<String, Object> params, Map<String, String> headers) {
|
|
|
+ try {
|
|
|
+ CloseableHttpClient httpClient = getHttpClient();
|
|
|
+ StringBuilder urlBuilder = new StringBuilder(url);
|
|
|
+ int i = 0;
|
|
|
+ if (!CollectionUtils.isEmpty(params)) for (String key : params.keySet()) {
|
|
|
+ urlBuilder.append((i == 0) ? "?" : "&");
|
|
|
+ urlBuilder.append(key).append("=");
|
|
|
+ if (params.get(key) != null) {
|
|
|
+ urlBuilder.append(URLEncoder.encode(String.valueOf(params.get(key)), "utf-8"));
|
|
|
+ }
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpGet httpGet = new HttpGet(urlBuilder.toString());
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(headers)) {
|
|
|
+ for (String key : headers.keySet()) {
|
|
|
+ httpGet.setHeader(key, headers.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ CloseableHttpResponse response = httpClient.execute(httpGet);
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ String content = EntityUtils.toString(entity);
|
|
|
+ EntityUtils.consume(entity);
|
|
|
+ return content;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOGGER.error("ERROR, call http get" + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理POST类请求,表格类型
|
|
|
+ */
|
|
|
+ public static String doPost(String url, Map<String, String> params) {
|
|
|
+ return doPost(url, params, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理POST类请求
|
|
|
+ public static String doPost(String url, Object object) {
|
|
|
+ return doPost(url, object, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理POST类请求,body类型
|
|
|
+ */
|
|
|
+ public static String doPost(String url, Object body, Map<String, String> headers) {
|
|
|
+ try {
|
|
|
+ CloseableHttpClient httpClient = getHttpClient();
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ httpPost.setHeader("Accept", "application/json; charset=UTF-8");
|
|
|
+ httpPost.setHeader("Content-Type", "application/json; charset=UTF-8");
|
|
|
+ RequestConfig config = RequestConfig.custom()
|
|
|
+ .setConnectionRequestTimeout(30000)
|
|
|
+ .setConnectTimeout(30000).setSocketTimeout(30000).build();
|
|
|
+
|
|
|
+
|
|
|
+ if (body == null) {
|
|
|
+ body = new Object();
|
|
|
+ }
|
|
|
+
|
|
|
+ StringEntity strEntity = new StringEntity(JsonUtils.getJsonString(body),
|
|
|
+ Charset.forName("UTF-8"));
|
|
|
+ httpPost.setEntity(strEntity);
|
|
|
+ httpPost.setConfig(config);
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(headers)) {
|
|
|
+ for (Entry<String, String> entry : headers.entrySet()) {
|
|
|
+ httpPost.setHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ CloseableHttpResponse response = httpClient.execute(httpPost);
|
|
|
+ //资源创建成功 返回状态码201 返回数据为空
|
|
|
+ if(response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED){
|
|
|
+ return CommonConstant.RESULT_SUCCESS;
|
|
|
+ }
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ String content = EntityUtils.toString(entity);
|
|
|
+ EntityUtils.consume(entity);
|
|
|
+ return content;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOGGER.error("ERROR, call http post" + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理POST类请求,表格类型
|
|
|
+ */
|
|
|
+ public static String doPost(String url, Map<String, String> params, Map<String, String> headers) {
|
|
|
+ try {
|
|
|
+ CloseableHttpClient httpClient = getHttpClient();
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ httpPost.setHeader("Accept", "application/json; charset=UTF-8");
|
|
|
+ RequestConfig config = RequestConfig.custom()
|
|
|
+ .setConnectionRequestTimeout(60000)
|
|
|
+ .setConnectTimeout(30000).setSocketTimeout(30000).build();
|
|
|
+
|
|
|
+ List<BasicNameValuePair> formParams = new ArrayList<>();
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(params)) {
|
|
|
+ for (Entry<String, String> entry : params.entrySet()) {
|
|
|
+ formParams.add(new BasicNameValuePair(entry.getKey(), entry
|
|
|
+ .getValue()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ StringEntity strEntity = new UrlEncodedFormEntity(formParams,
|
|
|
+ Charset.forName("UTF-8"));
|
|
|
+ httpPost.setEntity(strEntity);
|
|
|
+ httpPost.setConfig(config);
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(headers)) {
|
|
|
+ for (Entry<String, String> entry : headers.entrySet()) {
|
|
|
+ httpPost.setHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ CloseableHttpResponse response = httpClient.execute(httpPost);
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ String content = EntityUtils.toString(entity);
|
|
|
+ EntityUtils.consume(entity);
|
|
|
+ return content;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOGGER.error("ERROR, call http post" + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String doPut(String url, Map<String, Object> params) {
|
|
|
+ return doPut(url, params, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String doPut(String url, Map<String, Object> params, Map<String, String> headers) {
|
|
|
+ CloseableHttpClient httpClient = getHttpClient();
|
|
|
+ CloseableHttpResponse response;
|
|
|
+ try {
|
|
|
+
|
|
|
+ StringBuilder urlBuilder = new StringBuilder(url);
|
|
|
+ int i = 0;
|
|
|
+ if (!CollectionUtils.isEmpty(params)) {
|
|
|
+ for (Entry<String, Object> entry : params.entrySet()) {
|
|
|
+ urlBuilder.append((i == 0) ? "?" : "&");
|
|
|
+ urlBuilder.append(entry.getKey()).append("=").append(entry.getValue());
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ HttpPut httpPut = new HttpPut(urlBuilder.toString());
|
|
|
+ if (!CollectionUtils.isEmpty(headers)) {
|
|
|
+ for (Entry<String, String> entry : headers.entrySet()) {
|
|
|
+ httpPut.setHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ response = httpClient.execute(httpPut);
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ String content = EntityUtils.toString(entity);
|
|
|
+ EntityUtils.consume(entity);
|
|
|
+ return content;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOGGER.error("ERROR, call http put" + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * contentType 为text/plain 或application/json
|
|
|
+ * @param url
|
|
|
+ * @param body
|
|
|
+ * @param headers
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String doPut(String url, Object body, Map<String, String> headers,String contentType) {
|
|
|
+ CloseableHttpClient httpClient = getHttpClient();
|
|
|
+ CloseableHttpResponse response;
|
|
|
+ HttpPut httpPut = new HttpPut(url);
|
|
|
+ httpPut.setHeader("Content-Type", contentType+"; charset=UTF-8");
|
|
|
+ try {
|
|
|
+ if (body == null) {
|
|
|
+ body = new Object();
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(headers)) {
|
|
|
+ for (Entry<String, String> entry : headers.entrySet()) {
|
|
|
+ httpPut.setHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ StringEntity strEntity ;
|
|
|
+ if(contentType.equals("application/json")){
|
|
|
+ strEntity = new StringEntity(JsonUtils.getJsonString(body), Charset.forName("UTF-8"));
|
|
|
+ }else{
|
|
|
+ strEntity = new StringEntity(body.toString());
|
|
|
+ }
|
|
|
+ httpPut.setEntity(strEntity);
|
|
|
+ response = httpClient.execute(httpPut);
|
|
|
+ if(response.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT){
|
|
|
+ return CommonConstant.RESULT_SUCCESS;
|
|
|
+ }
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ String content = EntityUtils.toString(entity);
|
|
|
+ EntityUtils.consume(entity);
|
|
|
+ return content;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOGGER.error("ERROR, call http put" + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String doDelete(String url) {
|
|
|
+ return doDelete(url, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String doDelete(String url, Map<String, Object> params) {
|
|
|
+ return doDelete(url, params, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String doDelete(String url, Map<String, Object> params, Map<String, String> headers) {
|
|
|
+ try {
|
|
|
+ CloseableHttpClient httpClient = getHttpClient();
|
|
|
+ StringBuilder urlBuilder = new StringBuilder(url);
|
|
|
+ int i = 0;
|
|
|
+ if (!CollectionUtils.isEmpty(params)) for (String key : params.keySet()) {
|
|
|
+ urlBuilder.append((i == 0) ? "?" : "&");
|
|
|
+ urlBuilder.append(key).append("=").append(params.get(key));
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ HttpDelete httpDelete = new HttpDelete(urlBuilder.toString());
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(headers)) {
|
|
|
+ for (String key : headers.keySet()) {
|
|
|
+ httpDelete.setHeader(key, headers.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ CloseableHttpResponse response = httpClient.execute(httpDelete);
|
|
|
+ if(response.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT){
|
|
|
+ return CommonConstant.STRING_EMPTY;
|
|
|
+ }
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ String content = EntityUtils.toString(entity);
|
|
|
+ EntityUtils.consume(entity);
|
|
|
+ return content;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOGGER.error("ERROR, call http get" + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|