package com.mgm.mcrm.common.services; import com.alibaba.fastjson.JSON; import com.mgm.mcrm.common.dtos.HttpResult; import com.mgm.mcrm.common.dtos.config.ApiGatewaySetting; import com.mgm.mcrm.common.enums.HttpMethods; import com.mgm.mcrm.common.httpclient.HttpClientExecutor; import com.mgm.mcrm.common.httpclient.HttpRequestOptions; import jakarta.annotation.Resource; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.core5.http.Header; import org.apache.hc.core5.http.message.BasicHeader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; /** * 請求服務 * @author Aaka */ @Service public class HttpService { private static final Logger log = LoggerFactory.getLogger(HttpService.class); @Resource @Qualifier("httpClientWithSSL") private CloseableHttpClient httpClient; @Resource @Qualifier("httpRequestConfig") private RequestConfig requestConfig; @Resource private ApiGatewaySetting apiGatewaySetting; private Header[] supplementDaylightProxySecurity(Header[] headers){ List
newHeaders = (headers == null ? new ArrayList<>() : new ArrayList<>(Arrays.asList(headers))); newHeaders.add(new BasicHeader("application-id",apiGatewaySetting.getDaylightAppId())); newHeaders.add(new BasicHeader("Authorization",apiGatewaySetting.getDaylightAppSecret())); return newHeaders.toArray(new Header[0]); } public String sslGet(String url, Header[] headers, Map map) throws Exception { String daylightUrl=apiGatewaySetting.getDaylightProxyUrl(); // todo 在這裏需要把請求頭的信息插入到數據庫裏 log.info("======sslGet======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map)); // 使用 HttpRequestOptions 配置 GET 请求(带参数) HttpRequestOptions options = HttpRequestOptions.create() .url(daylightUrl + url) .timeout(requestConfig) .map(map) .encoding("utf-8") .client(httpClient); String result = HttpClientExecutor.get(options, supplementDaylightProxySecurity(headers)); log.info("====== GET request result:{}", result); return result; } /** * 使用 HttpConfig 配置 POST 请求(form-urlencoded 格式) * @param url 請求路徑 * @param headers 請求頭 * @param map 請求參數 * @return 接口結果的json數據 * @throws Exception 異常 */ public String sslPostForm(String url, Header[] headers, Map map) throws Exception { String daylightUrl=apiGatewaySetting.getDaylightProxyUrl(); log.info("======sslPostForm======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map)); // 使用 HttpRequestOptions 配置 POST 请求(form-urlencoded 格式) HttpRequestOptions options = HttpRequestOptions.create() .url(daylightUrl + url) .timeout(requestConfig) .method(HttpMethods.POST) .map(map) // 使用 map,会自动转换为 form-urlencoded .encoding("utf-8") .client(httpClient); String result = HttpClientExecutor.post(options, supplementDaylightProxySecurity(headers)); log.info("====== POST Form request result:{}", result); return result; } /** * 使用 HttpConfig 配置 POST 请求(JSON 格式) * @param url 請求路徑 * @param headers 請求頭 * @param map 請求參數 * @return 接口結果的json數據 * @throws Exception 異常 */ public String sslPostJson(String url, Header[] headers, Map map) throws Exception { String daylightUrl=apiGatewaySetting.getDaylightProxyUrl(); log.info("======sslPostJson======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map)); // 使用 HttpRequestOptions 配置 POST 请求(JSON 格式) HttpRequestOptions options = HttpRequestOptions.create() .url(daylightUrl + url) .timeout(requestConfig) .method(HttpMethods.POST) .json(JSON.toJSONString(map)) // 使用 map,会自动转换为 json .encoding("utf-8") .client(httpClient); String result = HttpClientExecutor.post(options, supplementDaylightProxySecurity(headers)); log.info("====== POST Json request result:{}", result); return result; } public String sslPostJsonContent(String url, Header[] headers, String jsonContent) throws Exception { String daylightUrl=apiGatewaySetting.getDaylightProxyUrl(); // 使用 HttpRequestOptions 配置 POST 请求(JSON 格式) HttpRequestOptions options = HttpRequestOptions.create() .url(daylightUrl + url) .timeout(requestConfig) .method(HttpMethods.POST) .json(jsonContent) .encoding("utf-8") .client(httpClient); return HttpClientExecutor.post(options, supplementDaylightProxySecurity(headers)); } /** * 使用 HttpConfig 配置 PUT 请求 * @param url 請求路徑 * @param headers 請求頭 * @param map 請求參數 * @return 接口結果的json數據 * @throws Exception 異常 */ public String sslPut(String url, Header[] headers, Map map) throws Exception { String daylightUrl=apiGatewaySetting.getDaylightProxyUrl(); log.info("======sslPut======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map)); HttpRequestOptions options = HttpRequestOptions.create() .url(daylightUrl + url) .timeout(requestConfig) .method(HttpMethods.PUT) .json(JSON.toJSONString(map)) .encoding("utf-8") .client(httpClient); String result = HttpClientExecutor.execute(options, supplementDaylightProxySecurity(headers)); log.info("====== PUT request result:{}", result); return result; } /** * 使用 HttpConfig 配置 Delete 请求 * @param url 請求路徑 * @param headers 請求頭 * @param map 請求參數 * @return 接口結果的json數據 * @throws Exception 異常 */ public String sslPutDelete(String url, Header[] headers, Map map) throws Exception { String daylightUrl=apiGatewaySetting.getDaylightProxyUrl(); log.info("======sslPutDelete======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map)); HttpRequestOptions options = HttpRequestOptions.create() .url(daylightUrl + url) .timeout(requestConfig) .method(HttpMethods.DELETE) .client(httpClient); String result = HttpClientExecutor.execute(options, supplementDaylightProxySecurity(headers)); System.out.println("DELETE 请求结果:\n" + result); log.info("====== DELETE request result:{}", result); return result; } /** * 使用 HttpConfig 配置 Delete 请求 * @param url 請求路徑 * @param headers 請求頭 * @param map 要传的数据 * @return 接口結果的json數據 * @throws Exception 異常 */ public HttpResult sslPostHttpResult(String url, Header[] headers, Map map) throws Exception { String daylightUrl=apiGatewaySetting.getDaylightProxyUrl(); log.info("======sslPutDelete======url:{}===========headers:{}", daylightUrl + url,JSON.toJSONString(headers)); HttpRequestOptions postConfig = HttpRequestOptions.create() .url(daylightUrl + url) .timeout(requestConfig) .json(JSON.toJSONString(map)) .method(HttpMethods.POST) .client(httpClient); HttpResult result = HttpClientExecutor.sendAndPostResp(postConfig, supplementDaylightProxySecurity(headers)); System.out.println("POST 请求结果:\n" + result); log.info("====== POST request result:{}", result); return result; } public HttpResult sslGetHttpResult(String url, Header[] headers, Map map) throws Exception { String daylightUrl=apiGatewaySetting.getDaylightProxyUrl(); // todo 在這裏需要把請求頭的信息插入到數據庫裏 log.info("======sslGet======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map)); // 使用 HttpConfig 配置 GET 请求(带参数) HttpRequestOptions getConfig = HttpRequestOptions.create() .url(daylightUrl + url) .timeout(requestConfig) .map(map) .encoding("utf-8") .client(httpClient); HttpResult result = HttpClientExecutor.sendAndGetResp(getConfig, supplementDaylightProxySecurity(headers)); log.info("====== GET request result:{}", result.getResult()); Map mapResult = JSON.parseObject(result.getResult(), Map.class); log.info("====== GET request result:{}", mapResult.get("results")); return result; } }