diff --git a/backend/src/main/java/io/dataease/commons/utils/HttpClientConfig.java b/backend/src/main/java/io/dataease/commons/utils/HttpClientConfig.java new file mode 100755 index 0000000000..0f3e5961bc --- /dev/null +++ b/backend/src/main/java/io/dataease/commons/utils/HttpClientConfig.java @@ -0,0 +1,72 @@ +package io.dataease.commons.utils; + +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.config.RequestConfig.Builder; + +import java.util.HashMap; +import java.util.Map; + +public class HttpClientConfig { + + // 字符集 + private String charset = "UTF-8"; + + // 请求头 + private Map header = new HashMap<>(); + + // 设置连接超时时间,单位毫秒 + private int connectTimeout = 5000; + // 设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的 + private int connectionRequestTimeout = 5000; + // 请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用 + private int cocketTimeout = 60000; + + public RequestConfig buildRequestConfig() { + Builder builder = RequestConfig.custom(); + builder.setConnectTimeout(connectTimeout); + builder.setConnectionRequestTimeout(connectionRequestTimeout); + builder.setSocketTimeout(cocketTimeout); + return builder.build(); + } + + public String getCharset() { + return charset; + } + + public void setCharset(String charset) { + this.charset = charset; + } + + public Map getHeader() { + return header; + } + + public void addHeader(String key, String value) { + header.put(key, value); + } + + public int getConnectTimeout() { + return connectTimeout; + } + + public void setConnectTimeout(int connectTimeout) { + this.connectTimeout = connectTimeout; + } + + public int getConnectionRequestTimeout() { + return connectionRequestTimeout; + } + + public void setConnectionRequestTimeout(int connectionRequestTimeout) { + this.connectionRequestTimeout = connectionRequestTimeout; + } + + public int getCocketTimeout() { + return cocketTimeout; + } + + public void setCocketTimeout(int cocketTimeout) { + this.cocketTimeout = cocketTimeout; + } + +} diff --git a/backend/src/main/java/io/dataease/commons/utils/HttpClientUtil.java b/backend/src/main/java/io/dataease/commons/utils/HttpClientUtil.java new file mode 100755 index 0000000000..ac6e4552fc --- /dev/null +++ b/backend/src/main/java/io/dataease/commons/utils/HttpClientUtil.java @@ -0,0 +1,207 @@ +package io.dataease.commons.utils; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.entity.EntityBuilder; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.conn.ssl.TrustSelfSignedStrategy; +import org.apache.http.conn.ssl.TrustStrategy; +import org.apache.http.entity.ContentType; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.protocol.HTTP; +import org.apache.http.ssl.SSLContexts; +import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLContext; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class HttpClientUtil { + + private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class); + + private static final String HTTPS = "https"; + + /** + * 根据url构建HttpClient(区分http和https) + * + * @param url 请求地址 + * @return CloseableHttpClient实例 + */ + private static CloseableHttpClient buildHttpClient(String url) { + try { + if (url.startsWith(HTTPS)) { + // https 增加信任设置 + TrustStrategy trustStrategy = new TrustSelfSignedStrategy(); + SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(trustStrategy).build(); + HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE; + return HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(hostnameVerifier).build(); + } else { + // http + return HttpClientBuilder.create().build(); + } + } catch (Exception e) { + throw new RuntimeException("HttpClient构建失败", e); + } + } + + /** + * Get http请求 + * + * @param url 请求地址 + * @param config 配置项,如果null则使用默认配置 + * @return 响应结果字符串 + */ + public static String get(String url, HttpClientConfig config) { + CloseableHttpClient httpClient = buildHttpClient(url); + HttpGet httpGet = new HttpGet(url); + + if (config == null) { + config = new HttpClientConfig(); + } + try { + httpGet.setConfig(config.buildRequestConfig()); + + Map header = config.getHeader(); + for (String key : header.keySet()) { + httpGet.addHeader(key, header.get(key)); + } + + httpGet.addHeader(HTTP.CONTENT_ENCODING, config.getCharset()); + + HttpResponse response = httpClient.execute(httpGet); + HttpEntity entity = response.getEntity(); + return EntityUtils.toString(entity, config.getCharset()); + } catch (Exception e) { + logger.error("HttpClient查询失败", e); + throw new RuntimeException("HttpClient查询失败", e); + } finally { + try { + httpClient.close(); + } catch (Exception e) { + logger.error("HttpClient关闭连接失败", e); + } + } + } + + /** + * Post请求,请求内容必须为JSON格式的字符串 + * + * @param url 请求地址 + * @param config 配置项,如果null则使用默认配置 + * @param json JSON格式的字符串 + * @return 响应结果字符串 + */ + public static String post(String url, String json, HttpClientConfig config) { + CloseableHttpClient httpClient = buildHttpClient(url); + HttpPost httpPost = new HttpPost(url); + if (config == null) { + config = new HttpClientConfig(); + } + try { + httpPost.setConfig(config.buildRequestConfig()); + + Map header = config.getHeader(); + for (String key : header.keySet()) { + httpPost.addHeader(key, header.get(key)); + } + httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json"); + httpPost.addHeader(HTTP.CONTENT_ENCODING, config.getCharset()); + + EntityBuilder entityBuilder = EntityBuilder.create(); + entityBuilder.setText(json); + entityBuilder.setContentType(ContentType.APPLICATION_JSON); + entityBuilder.setContentEncoding(config.getCharset()); + HttpEntity requestEntity = entityBuilder.build(); + httpPost.setEntity(requestEntity); + + HttpResponse response = httpClient.execute(httpPost); + HttpEntity entity = response.getEntity(); + return EntityUtils.toString(entity, config.getCharset()); + } catch (Exception e) { + logger.error("HttpClient查询失败", e); + throw new RuntimeException("HttpClient查询失败", e); + } finally { + try { + httpClient.close(); + } catch (Exception e) { + logger.error("HttpClient关闭连接失败", e); + } + } + } + + /** + * Post请求,请求内容必须为JSON格式的字符串 + * + * @param url 请求地址 + * @param json JSON格式的字符串 + * @return 响应结果字符串 + */ + public static String post(String url, String json) { + return HttpClientUtil.post(url, json, null); + } + + /** + * Post请求,请求内容必须为键值对参数 + * + * @param url 请求地址 + * @param config 配置项,如果null则使用默认配置 + * @param body 请求内容键值对参数 + * @return 响应结果字符串 + */ + public static String post(String url, Map body, HttpClientConfig config) { + CloseableHttpClient httpClient = buildHttpClient(url); + HttpPost httpPost = new HttpPost(url); + if (config == null) { + config = new HttpClientConfig(); + } + try { + httpPost.setConfig(config.buildRequestConfig()); + + Map header = config.getHeader(); + for (String key : header.keySet()) { + httpPost.addHeader(key, header.get(key)); + } + httpPost.addHeader(HTTP.CONTENT_ENCODING, config.getCharset()); + + if (body != null && body.size() > 0) { + List nvps = new ArrayList<>(); + for (String key : body.keySet()) { + nvps.add(new BasicNameValuePair(key, body.get(key))); + } + try { + httpPost.setEntity(new UrlEncodedFormEntity(nvps, config.getCharset())); + } catch (Exception e) { + logger.error("HttpClient转换编码错误", e); + throw new RuntimeException("HttpClient转换编码错误", e); + } + } + + HttpResponse response = httpClient.execute(httpPost); + HttpEntity entity = response.getEntity(); + return EntityUtils.toString(entity, config.getCharset()); + } catch (Exception e) { + logger.error("HttpClient查询失败", e); + throw new RuntimeException("HttpClient查询失败", e); + } finally { + try { + httpClient.close(); + } catch (Exception e) { + logger.error("HttpClient关闭连接失败", e); + } + } + } + + +}