diff --git a/ruoyi-admin/src/main/java/com/ruoyi/api/labour/controller/LabourApiController.java b/ruoyi-admin/src/main/java/com/ruoyi/api/labour/controller/LabourApiController.java index 57d80762..f3741bf2 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/api/labour/controller/LabourApiController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/api/labour/controller/LabourApiController.java @@ -1,6 +1,7 @@ package com.ruoyi.api.labour.controller; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.ruoyi.api.base.TokenReqVo; import com.ruoyi.api.labour.domain.LabourSignetVo; @@ -17,8 +18,11 @@ import com.ruoyi.common.enums.LimitType; import com.ruoyi.common.enums.ShiFouEnum; import com.ruoyi.common.enums.UserTypeEnum; import com.ruoyi.common.exception.ServiceException; +import com.ruoyi.common.utils.AuthRsaUtils; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.http.HttpClientUtil; +import com.ruoyi.common.utils.sign.Md5Utils; import com.ruoyi.framework.web.service.SysLoginService; import com.ruoyi.system.domain.SysApplyConfig; import com.ruoyi.system.service.ISysUserService; @@ -38,9 +42,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; +import java.util.*; /** * 劳务人员APIController @@ -442,4 +444,217 @@ public class LabourApiController extends BaseController { return success(failServiceIdList); } + private static final String publicKey = ""; + private static final String baseUrl = "http://localhost:8090/jhapi"; + private static final String token = "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6Ijk1OWIwODAzLTU3NjEtNDBlOS1iYWUxLWJhNzQ0NjkyNmNkMCJ9.BKEaL5vGknNX6aVwYqG1xlJqv9haByoCD4Qo5alXXPWY3PkQsyKuLCN-Iw2ZHM3B4MX9XahrS2sT9Y3RjmuWXA"; + + /** + * 推送,修改班组信息【示例代码】 + */ + public static String getToken(){ + String timestamp = String.valueOf(System.currentTimeMillis()); + String appId = "jhcf170515993052073qrDFKJmk001"; + try { + // 明文信息->公钥加密 + String encryptByPublicKeyStr = AuthRsaUtils.encryptByPublicKey(publicKey, appId+timestamp); + log.info("公钥加密...{}" , encryptByPublicKeyStr); + // redis中查询->未查询到则重新获取 + String url = baseUrl + "/api/labour/v1/getToken"; + Map body = new HashMap<>(); + body.put("appId",appId); + body.put("sign",encryptByPublicKeyStr); + body.put("timestamp",timestamp); + String result = HttpClientUtil.doPost(url, null, com.alibaba.fastjson2.JSON.toJSONString(body)); + JSONObject jsonObject = JSONObject.parseObject(result); + if(jsonObject.getInteger("code")==200){ + // 获取到存入redis,设置30分钟有效期 + String token = jsonObject.getString("Authorization"); + log.info("Authorization...{}" , token); + return token; + } + }catch (Exception e){ + e.printStackTrace(); + } + return null; + } + + /** + * 推送,修改班组信息【示例代码】 + */ + public static void pushLabourGroup(){ + Map map = new HashMap<>(); + map.put("serverid","1"); + String jsonString = JSONObject.toJSONString(map); + String timestamp = String.valueOf(System.currentTimeMillis()); + try { + // 明文信息->公钥加密 + String encryptByPublicKeyStr = AuthRsaUtils.encryptByPublicKey(publicKey, Md5Utils.hash(jsonString)+timestamp); + log.info("公钥加密...{}" , encryptByPublicKeyStr); + String url = baseUrl + "/api/labour/v1/pushLabourGroup"; + Map body = new HashMap<>(); + body.put("sign",encryptByPublicKeyStr); + body.put("data",jsonString); + body.put("timestamp",timestamp); + // redis中查询->未查询到则重新获取 + Map headers = new HashMap<>(); + headers.put("Authorization", token); + String result = HttpClientUtil.doPost(url, headers, com.alibaba.fastjson2.JSON.toJSONString(body)); + JSONObject jsonObject = JSONObject.parseObject(result); + if(jsonObject.getInteger("code")==200){ + log.info("success...{}"); + } + }catch (Exception e){ + e.printStackTrace(); + } + } + + /** + * 批量推送班组信息【示例代码】 + */ + public static void pushLabourGroupList(List> datas){ + String jsonString = JSONObject.toJSONString(datas); + String timestamp = String.valueOf(System.currentTimeMillis()); + try { + // 明文信息->公钥加密 + String encryptByPublicKeyStr = AuthRsaUtils.encryptByPublicKey(publicKey, Md5Utils.hash(jsonString)+timestamp); + log.info("公钥加密...{}" , encryptByPublicKeyStr); + String url = baseUrl + "/api/labour/v1/pushLabourGroup"; + Map body = new HashMap<>(); + body.put("sign",encryptByPublicKeyStr); + body.put("data",jsonString); + body.put("timestamp",timestamp); + // redis中查询->未查询到则重新获取 + Map headers = new HashMap<>(); + headers.put("Authorization", token); + String result = HttpClientUtil.doPost(url, headers, com.alibaba.fastjson2.JSON.toJSONString(body)); + JSONObject jsonObject = JSONObject.parseObject(result); + if(jsonObject.getInteger("code")==200){ + JSONArray jsonArray = jsonObject.getJSONArray("data"); + log.info("推送失败下标...{}",jsonArray); + } + }catch (Exception e){ + e.printStackTrace(); + } + } + + /** + * 推送,修改人员信息【示例代码】 + */ + public static void pushLabourUser(){ + Map map = new HashMap<>(); + map.put("workerId","1"); + map.put("name","张三"); + String jsonString = JSONObject.toJSONString(map); + String timestamp = String.valueOf(System.currentTimeMillis()); + try { + // 明文信息->公钥加密 + String encryptByPublicKeyStr = AuthRsaUtils.encryptByPublicKey(publicKey, Md5Utils.hash(jsonString)+timestamp); + log.info("公钥加密...{}" , encryptByPublicKeyStr); + String url = baseUrl + "/api/labour/v1/pushLabourUser"; + Map body = new HashMap<>(); + body.put("sign",encryptByPublicKeyStr); + body.put("data",jsonString); + body.put("timestamp",timestamp); + // redis中查询->未查询到则重新获取 + Map headers = new HashMap<>(); + headers.put("Authorization", token); + String result = HttpClientUtil.doPost(url, headers, com.alibaba.fastjson2.JSON.toJSONString(body)); + JSONObject jsonObject = JSONObject.parseObject(result); + if(jsonObject.getInteger("code")==200){ + log.info("success...{}"); + } + }catch (Exception e){ + e.printStackTrace(); + } + } + + /** + * 批量推送人员信息【示例代码】 + */ + public static void pushLabourUserList(List> datas){ + String jsonString = JSONObject.toJSONString(datas); + String timestamp = String.valueOf(System.currentTimeMillis()); + try { + // 明文信息->公钥加密 + String encryptByPublicKeyStr = AuthRsaUtils.encryptByPublicKey(publicKey, Md5Utils.hash(jsonString)+timestamp); + log.info("公钥加密...{}" , encryptByPublicKeyStr); + String url = baseUrl + "/api/labour/v1/pushLabourUserList"; + Map body = new HashMap<>(); + body.put("sign",encryptByPublicKeyStr); + body.put("data",jsonString); + body.put("timestamp",timestamp); + // redis中查询->未查询到则重新获取 + Map headers = new HashMap<>(); + headers.put("Authorization", token); + String result = HttpClientUtil.doPost(url, headers, com.alibaba.fastjson2.JSON.toJSONString(body)); + JSONObject jsonObject = JSONObject.parseObject(result); + if(jsonObject.getInteger("code")==200){ + JSONArray jsonArray = jsonObject.getJSONArray("data"); + log.info("推送失败下标...{}",jsonArray); + } + }catch (Exception e){ + e.printStackTrace(); + } + } + + /** + * 推送,修改班组人员出勤信息【示例代码】 + */ + public static void pushLabourData(){ + Map map = new HashMap<>(); + map.put("serverid","1"); + map.put("workerId","1"); + String jsonString = JSONObject.toJSONString(map); + String timestamp = String.valueOf(System.currentTimeMillis()); + try { + // 明文信息->公钥加密 + String encryptByPublicKeyStr = AuthRsaUtils.encryptByPublicKey(publicKey, Md5Utils.hash(jsonString)+timestamp); + log.info("公钥加密...{}" , encryptByPublicKeyStr); + String url = baseUrl + "/api/labour/v1/pushLabourData"; + Map body = new HashMap<>(); + body.put("sign",encryptByPublicKeyStr); + body.put("data",jsonString); + body.put("timestamp",timestamp); + // redis中查询->未查询到则重新获取 + Map headers = new HashMap<>(); + headers.put("Authorization", token); + String result = HttpClientUtil.doPost(url, headers, com.alibaba.fastjson2.JSON.toJSONString(body)); + JSONObject jsonObject = JSONObject.parseObject(result); + if(jsonObject.getInteger("code")==200){ + log.info("success...{}"); + } + }catch (Exception e){ + e.printStackTrace(); + } + } + + /** + * 批量推送班组人员出勤信息【示例代码】 + */ + public static void pushLabourDataList(List> datas){ + String jsonString = JSONObject.toJSONString(datas); + String timestamp = String.valueOf(System.currentTimeMillis()); + try { + // 明文信息->公钥加密 + String encryptByPublicKeyStr = AuthRsaUtils.encryptByPublicKey(publicKey, Md5Utils.hash(jsonString)+timestamp); + log.info("公钥加密...{}" , encryptByPublicKeyStr); + String url = baseUrl + "/api/labour/v1/pushLabourDataList"; + Map body = new HashMap<>(); + body.put("sign",encryptByPublicKeyStr); + body.put("data",jsonString); + body.put("timestamp",timestamp); + // redis中查询->未查询到则重新获取 + Map headers = new HashMap<>(); + headers.put("Authorization", token); + String result = HttpClientUtil.doPost(url, headers, com.alibaba.fastjson2.JSON.toJSONString(body)); + JSONObject jsonObject = JSONObject.parseObject(result); + if(jsonObject.getInteger("code")==200){ + JSONArray jsonArray = jsonObject.getJSONArray("data"); + log.info("推送失败下标...{}",jsonArray); + } + }catch (Exception e){ + e.printStackTrace(); + } + } + } diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 7297008c..8c31d619 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -163,6 +163,12 @@ true + + org.apache.httpcomponents + httpclient + 4.5.13 + + \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/enums/HttpStatusEnum.java b/ruoyi-common/src/main/java/com/ruoyi/common/enums/HttpStatusEnum.java index 90274890..d22f8a3a 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/enums/HttpStatusEnum.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/enums/HttpStatusEnum.java @@ -7,7 +7,7 @@ package com.ruoyi.common.enums; */ public enum HttpStatusEnum { - ERROR(11110, "AppId不存在或已被停用"), + ERROR(11110, "AppId不存在或已被删除"), DISABLE(11112, "AppId已被停用"), SINGET_ERROR(11113, "签名值不正确"), SINGET_TIMEOUT(11114, "签名数据已过期"), diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpClientUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpClientUtil.java new file mode 100644 index 00000000..5586b198 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpClientUtil.java @@ -0,0 +1,516 @@ +package com.ruoyi.common.utils.http; + +import com.ruoyi.common.utils.StringUtils; +import lombok.extern.slf4j.Slf4j; +import org.apache.http.Header; +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.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +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.BasicNameValuePair; +import org.apache.http.protocol.HTTP; +import org.apache.http.util.EntityUtils; +import org.springframework.http.MediaType; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * HttpClient工具类 + * + * @author: xingxixingzhe + * @date: 2023/9/25 14:18 + */ +@Slf4j +public class HttpClientUtil { + + public static final String HTTP_CHARSET = "utf-8"; + private static final String CONTENTTYPE = "Content-Type"; + + /** + * get请求 + * + * @param url 请求地址 + * @param headers 请求头,可以为null + * @return String + */ + public static String doGet(String url, Map headers) { + // 创建HttpClient对象 + CloseableHttpClient httpClient = HttpClients.createDefault(); + // 创建HttpResponse对象 + CloseableHttpResponse response = null; + log.info("request param info : {}", url); + log.info("request header info : {}", headers); + try { + try { + // 创建HttpPost对象 + HttpGet get = new HttpGet(url); + // 封装请求头 + if (null != headers) { + headers.keySet().forEach(key -> { + get.addHeader(key, headers.get(key)); + }); + } + response = httpClient.execute(get); + String result = EntityUtils.toString(response.getEntity(), Charset.forName(HTTP_CHARSET)); + log.info("response info : {}", result); + return result; + } catch (IOException e) { + log.error("get链接失败:" + e.getMessage()); + } finally { + // 释放连接 + if (response != null) { + response.close(); + } + } + } catch (IOException e) { + log.error("get关闭response失败:" + e.getMessage()); + } + return null; + } + + /** + * get请求 + * + * @param url 请求地址 + * @param headers 请求头,可以为null + * @param paramMap 请求参数 + * @return String + */ + public static String doGetQueryParams(String url, Map headers, Map paramMap) { + // 创建HttpClient对象 + CloseableHttpClient httpClient = HttpClients.createDefault(); + // 创建HttpResponse对象 + CloseableHttpResponse response = null; + StringBuilder urlTemp = new StringBuilder(url); + if (null != paramMap && paramMap.size() > 0) { + int index = 0; + for (Map.Entry entry : paramMap.entrySet()) { + if (index == 0) { + urlTemp.append("?").append(entry.getKey()).append("=").append(entry.getValue()); + } else { + urlTemp.append("&").append(entry.getKey()).append("=").append(entry.getValue()); + } + index++; + } + } + url = urlTemp.toString(); + log.info("request param info : {}", url); + log.info("request header info : {}", headers); + try { + try { + // 创建HttpPost对象 + HttpGet get = new HttpGet(url); + // 封装请求头 + if (null != headers) { + headers.keySet().forEach(key -> { + get.addHeader(key, headers.get(key)); + }); + } + response = httpClient.execute(get); + String result = EntityUtils.toString(response.getEntity(), Charset.forName(HTTP_CHARSET)); + log.info("response info : {}", result); + return result; + } catch (IOException e) { + log.error("get链接失败:" + e.getMessage()); + } finally { + // 释放连接 + if (response != null) { + response.close(); + } + } + } catch (IOException e) { + log.error("get关闭response失败:" + e.getMessage()); + } + return null; + } + + /** + * get请求 下载文件保存在本地 + * + * @param url 请求地址 + * @param headers 请求头,可以为null + * @param downloadPath 下载地址 + * @return String + */ + public static String doGetDownloadSaveLocal(String url, Map headers, String downloadPath) { + // 创建HttpClient对象 + CloseableHttpClient httpClient = HttpClients.createDefault(); + // 创建HttpResponse对象 + CloseableHttpResponse response = null; + log.info("request param info : {}", url); + log.info("request header info : {}", headers); + try { + try { + // 创建HttpPost对象 + HttpGet get = new HttpGet(url); + // 封装请求头 + if (null != headers) { + headers.keySet().forEach(key -> { + get.addHeader(key, headers.get(key)); + }); + } + response = httpClient.execute(get); + // 获取下载文件流 + InputStream inputStream = response.getEntity().getContent(); + // 获取下载文件的文件名,此处的 File-Name 头信息,需要在服务端进行自定义 + Header header = response.getFirstHeader("Content-Disposition"); + String fileName = header.getValue(); + fileName = StringUtils.substringAfter(fileName, "="); + fileName = System.currentTimeMillis() + fileName; + log.info("下载的文件名称:" + fileName); + File file = new File(downloadPath + fileName); + file.getParentFile().mkdirs(); + FileOutputStream fileOut = new FileOutputStream(file); + byte[] buffer = new byte[2048]; + int ch = 0; + while ((ch = inputStream.read(buffer)) != -1) { + fileOut.write(buffer, 0, ch); + } + inputStream.close(); + fileOut.flush(); + fileOut.close(); + return file.getPath(); + } catch (IOException e) { + log.error("get链接失败:" + e.getMessage()); + } finally { + // 释放连接 + if (response != null) { + response.close(); + } + } + } catch (IOException e) { + log.error("get关闭response失败:" + e.getMessage()); + } + + return null; + } + + /** + * get请求 带时间配置请求 + * + * @param url 请求地址 + * @param headers 请求头,可以为null + * @param connectTimeout 连接超时时间 + * @param connectionRequestTimeout 建立连接超时时间, + * @param socketTimeout 读取数据超时时间 + * @return String + */ + public static String doGet(String url, Map headers, + int connectTimeout, int connectionRequestTimeout, int socketTimeout) { + // 创建HttpClient对象 + CloseableHttpClient httpClient = HttpClients.createDefault(); + // 创建HttpResponse对象 + CloseableHttpResponse response = null; + log.info("request param info : {}", url); + log.info("request header info : {}", headers); + try { + try { + // 创建HttpPost对象 + HttpGet get = new HttpGet(url); + get.setConfig(RequestConfig.custom() + .setConnectTimeout(connectTimeout) + .setConnectionRequestTimeout(connectionRequestTimeout) + .setSocketTimeout(socketTimeout).build()); + // 封装请求头 + if (null != headers) { + headers.keySet().forEach(key -> { + get.addHeader(key, headers.get(key)); + }); + } + response = httpClient.execute(get); + String result = EntityUtils.toString(response.getEntity(), Charset.forName(HTTP_CHARSET)); + log.info("response info : {}", result); + return result; + } catch (IOException e) { + log.error("get链接失败:" + e.getMessage()); + } finally { + // 释放连接 + if (response != null) { + response.close(); + } + } + } catch (IOException e) { + log.error("get关闭response失败:" + e.getMessage()); + } + return null; + } + + /** + * post请求 body为json 带时间配置请求 + * + * @param url 请求地址 + * @param headers 请求头,可以为null + * @param body json请求体 + * @param connectTimeout 连接超时时间 + * @param connectionRequestTimeout 建立连接超时时间, + * @param socketTimeout 读取数据超时时间 + * @return String + * @author: lll + * @date: 2022年11月21日 14:11:49 + */ + public static String doPost(String url, Map headers, String body, + int connectTimeout, int connectionRequestTimeout, int socketTimeout) { + // 创建HttpClient对象 + CloseableHttpClient httpClient = HttpClients.createDefault(); + // 创建HttpResponse对象 + CloseableHttpResponse response = null; + log.info("request param info : {}", body); + log.info("request header info : {}", headers); + try { + try { + // 创建HttpPost对象 + HttpPost post = new HttpPost(url); + post.setConfig(RequestConfig.custom() + .setConnectTimeout(connectTimeout) + .setConnectionRequestTimeout(connectionRequestTimeout) + .setSocketTimeout(socketTimeout) + .build()); + // 封装请求头 + if (null != headers) { + headers.keySet().forEach(key -> { + post.addHeader(key, headers.get(key)); + }); + } + post.addHeader(HTTP.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + // 封装请求体 + if (StringUtils.isNotBlank(body)) { + // 请求体主要封装在HttpEntity中 + post.setEntity(new StringEntity(body, Charset.forName(HTTP_CHARSET))); + } + + response = httpClient.execute(post); + // 处理响应 + String result = EntityUtils.toString(response.getEntity(), Charset.forName(HTTP_CHARSET)); + log.info("response info : {}", result); + return result; + } catch (IOException e) { + log.error("post链接失败:" + e.getMessage()); + } finally { + // 释放连接 + if (response != null) { + response.close(); + } + } + } catch (IOException e) { + log.error("post关闭response失败:" + e.getMessage()); + } + return null; + } + + /** + * post请求 body为json + * + * @param url 请求地址 + * @param headers 请求头,可以为null + * @param body json请求体 + * @return String + */ + public static String doPost(String url, Map headers, String body) { + // 创建HttpClient对象 + CloseableHttpClient httpClient = HttpClients.createDefault(); + // 创建HttpResponse对象 + CloseableHttpResponse response = null; + log.info("request header url : {}", url); + log.info("request param body : {}", body); + log.info("request header headers : {}", headers); + try { + try { + // 创建HttpPost对象 + HttpPost post = new HttpPost(url); + // 封装请求头 + if (null != headers) { + headers.keySet().forEach(key -> { + post.addHeader(key, headers.get(key)); + }); + } + post.addHeader(HTTP.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + // 封装请求体 + if (StringUtils.isNotBlank(body)) { + // 请求体主要封装在HttpEntity中 + post.setEntity(new StringEntity(body, Charset.forName(HTTP_CHARSET))); + } + + response = httpClient.execute(post); + // 处理响应 + String result = EntityUtils.toString(response.getEntity(), Charset.forName(HTTP_CHARSET)); + log.info("response result : {}", result); + return result; + } catch (IOException e) { + log.error("post链接失败:" + e.getMessage()); + } finally { + // 释放连接 + if (response != null) { + response.close(); + } + } + } catch (IOException e) { + log.error("post关闭response失败:" + e.getMessage()); + } + return null; + } + + /** + * post请求 body为form + *

+ * post请求 body为json 带时间配置请求 + * + * @param url 请求地址 + * @param headers 请求头,可以为null + * @param body form请求体 + * @return String + */ + public static String doPost(String url, Map headers, Map body, + int connectTimeout, int connectionRequestTimeout, int socketTimeout) { + // 创建HttpClient对象 + CloseableHttpClient httpClient = HttpClients.createDefault(); + // 创建HttpResponse对象 + CloseableHttpResponse response = null; + log.info("request param info : {}", body); + log.info("request header info : {}", headers); + try { + try { + // 创建HttpPost对象 + HttpPost post = new HttpPost(url); + post.setConfig(RequestConfig.custom().setConnectTimeout(connectTimeout).setConnectionRequestTimeout(connectionRequestTimeout).setSocketTimeout(socketTimeout).build()); + // 封装请求头 + if (null != headers) { + headers.keySet().forEach(key -> { + post.addHeader(key, headers.get(key)); + }); + } + // 封装请求体 + List nvpList = new ArrayList<>(); + for (String name : body.keySet()) { + String value = String.valueOf(body.get(name)); + nvpList.add(new BasicNameValuePair(name, value)); + } + post.setEntity(new UrlEncodedFormEntity(nvpList)); + + response = httpClient.execute(post); + // 处理响应 + String result = EntityUtils.toString(response.getEntity(), Charset.forName(HTTP_CHARSET)); + log.info("response info : {}", result); + return result; + } catch (IOException e) { + log.error("post链接失败:" + e.getMessage()); + } finally { + // 释放连接 + if (response != null) { + response.close(); + } + } + } catch (IOException e) { + log.error("post关闭response失败:" + e.getMessage()); + } + return null; + } + + /** + * post请求 body为form + *

+ * + * @param url 请求地址 + * @param headers 请求头,可以为null + * @param body json请求体 + * @return String + */ + public static String doPost(String url, Map headers, Map body) { + // 创建HttpClient对象 + CloseableHttpClient httpClient = HttpClients.createDefault(); + // 创建HttpResponse对象 + CloseableHttpResponse response = null; + log.info("request param info : {}", body); + log.info("request header info : {}", headers); + try { + try { + // 创建HttpPost对象 + HttpPost post = new HttpPost(url); + // 封装请求头 + if (null != headers) { + headers.keySet().forEach(key -> { + post.addHeader(key, headers.get(key)); + }); + } + // 封装请求体 + List nvpList = new ArrayList<>(); + for (String name : body.keySet()) { + String value = String.valueOf(body.get(name)); + nvpList.add(new BasicNameValuePair(name, value)); + } + post.setEntity(new UrlEncodedFormEntity(nvpList,HTTP_CHARSET)); + + response = httpClient.execute(post); + // 处理响应 + String result = EntityUtils.toString(response.getEntity(), Charset.forName(HTTP_CHARSET)); + log.info("response info : {}", result); + return result; + } catch (IOException e) { + log.error("post链接失败:" + e.getMessage()); + } finally { + // 释放连接 + if (response != null) { + response.close(); + } + } + } catch (IOException e) { + log.error("post关闭response失败:" + e.getMessage()); + } + return null; + } + + /** + * delete请求 + * + * @param url 请求地址 + * @param headers 请求头,可以为null + * @return String + */ + public static String doDelete(String url, Map headers) { + // 创建HttpClient对象 + CloseableHttpClient httpClient = HttpClients.createDefault(); + // 创建HttpResponse对象 + CloseableHttpResponse response = null; + log.info("request param info : {}", url); + log.info("request header info : {}", headers); + try { + try { + // 创建HttpPost对象 + HttpDelete delete = new HttpDelete(url); + // 封装请求头 + if (null != headers) { + headers.keySet().forEach(key -> { + delete.addHeader(key, headers.get(key)); + }); + } + response = httpClient.execute(delete); + String result = EntityUtils.toString(response.getEntity(), Charset.forName(HTTP_CHARSET)); + log.info("response info : {}", result); + return result; + } catch (IOException e) { + log.error("delete链接失败:" + e.getMessage()); + } finally { + // 释放连接 + if (response != null) { + response.close(); + } + } + } catch (IOException e) { + log.error("delete关闭response失败:" + e.getMessage()); + } + return null; + } + +}