diff --git a/spring-boot-cps/cps-spring-boot-autoconfigure/src/main/java/in/hocg/boot/cps/autoconfiguration/impl/dataoke/lib/HttpUtil.java b/spring-boot-cps/cps-spring-boot-autoconfigure/src/main/java/in/hocg/boot/cps/autoconfiguration/impl/dataoke/lib/HttpUtil.java index af8706e3..02a8f43c 100644 --- a/spring-boot-cps/cps-spring-boot-autoconfigure/src/main/java/in/hocg/boot/cps/autoconfiguration/impl/dataoke/lib/HttpUtil.java +++ b/spring-boot-cps/cps-spring-boot-autoconfigure/src/main/java/in/hocg/boot/cps/autoconfiguration/impl/dataoke/lib/HttpUtil.java @@ -1,9 +1,8 @@ package in.hocg.boot.cps.autoconfiguration.impl.dataoke.lib; - +import cn.hutool.core.util.StrUtil; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; -import org.apache.http.client.HttpRequestRetryHandler; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; @@ -16,13 +15,14 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.BasicNameValuePair; -import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; +import org.apache.logging.log4j.util.Strings; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URISyntaxException; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -33,6 +33,7 @@ public class HttpUtil { private static PoolingHttpClientConnectionManager cm; private static String EMPTY_STR = ""; private static String UTF_8 = "UTF-8"; + private static void init() { if (cm == null) { cm = new PoolingHttpClientConnectionManager(); @@ -50,18 +51,13 @@ private static void init() { */ private static CloseableHttpClient getHttpClient() { init(); - return HttpClients.custom() - .setRetryHandler(new HttpRequestRetryHandler(){ - @Override - public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { - if (executionCount >= 1) { - // Do not retry if over max retry count - return false; - } - return true; - } - }) - .setConnectionManager(cm).setConnectionManagerShared(true).build(); + return HttpClients.custom().setRetryHandler((exception, executionCount, context) -> { + if (executionCount >= 1) { + // Do not retry if over max retry count + return false; + } + return true; + }).setConnectionManager(cm).setConnectionManagerShared(true).build(); } /** @@ -71,14 +67,14 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte public static String httpGetRequest(String url) { HttpGet httpGet = new HttpGet(url); RequestConfig config = RequestConfig.custom() - .setConnectTimeout(5000) //设置连接超时时间 - .setConnectionRequestTimeout(5000) // 设置请求超时时间 - .setSocketTimeout(5000) - .setRedirectsEnabled(false)//默认允许自动重定向 - .build(); + .setConnectTimeout(5000) //设置连接超时时间 + .setConnectionRequestTimeout(5000) // 设置请求超时时间 + .setSocketTimeout(5000) + .setRedirectsEnabled(false)//默认允许自动重定向 + .build(); httpGet.setConfig(config); httpGet.setHeader("Accept", "application/json"); - httpGet.addHeader("Accept-Encoding" ,"gzip"); //请求使用数据压缩 + httpGet.addHeader("Accept-Encoding", "gzip"); //请求使用数据压缩 return getResult(httpGet); } @@ -87,19 +83,19 @@ public static String httpGetRequest(String url, Map params) thro url = url.contains("?") ? url : url + "?"; HttpGet httpGet = new HttpGet(url + paramsStr); RequestConfig config = RequestConfig.custom() - .setConnectTimeout(5000) //设置连接超时时间 - .setConnectionRequestTimeout(5000) // 设置请求超时时间 - .setSocketTimeout(5000) - .setRedirectsEnabled(false)//默认允许自动重定向 - .build(); + .setConnectTimeout(5000) //设置连接超时时间 + .setConnectionRequestTimeout(5000) // 设置请求超时时间 + .setSocketTimeout(5000) + .setRedirectsEnabled(false)//默认允许自动重定向 + .build(); httpGet.setConfig(config); httpGet.setHeader("Accept", "application/json"); - httpGet.addHeader("Accept-Encoding" ,"gzip"); //请求使用数据压缩 + httpGet.addHeader("Accept-Encoding", "gzip"); //请求使用数据压缩 return getResult(httpGet); } public static String httpGetRequest(String url, Map headers, Map params) - throws URISyntaxException { + throws URISyntaxException { URIBuilder ub = new URIBuilder(); ub.setPath(url); ArrayList pairs = covertParams2NVPS(params); @@ -116,9 +112,9 @@ public static String httpPostRequest(String url) { return getResult(httpPost); } - public static String httpPostRequest(String url,String json) { + public static String httpPostRequest(String url, String json) { HttpPost httpPost = new HttpPost(url); - StringEntity entity = new StringEntity(json,"utf-8");//解决中文乱码问题 + StringEntity entity = new StringEntity(json, "utf-8");//解决中文乱码问题 entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); @@ -133,7 +129,7 @@ public static String httpPostRequest(String url, Map params) thr } public static String httpPostRequest(String url, Map headers, Map params) - throws UnsupportedEncodingException { + throws UnsupportedEncodingException { HttpPost httpPost = new HttpPost(url); for (Map.Entry param : headers.entrySet()) { httpPost.addHeader(param.getKey(), String.valueOf(param.getValue())); @@ -145,7 +141,7 @@ public static String httpPostRequest(String url, Map headers, Ma } private static ArrayList covertParams2NVPS(Map params) { - ArrayList pairs = new ArrayList(); + ArrayList pairs = new ArrayList<>(); for (Map.Entry param : params.entrySet()) { pairs.add(new BasicNameValuePair(param.getKey(), String.valueOf(param.getValue()))); } @@ -153,31 +149,23 @@ private static ArrayList covertParams2NVPS(Map pa } private static ArrayList covertParams2NVPSForStr(Map params) { - ArrayList pairs = new ArrayList(); + ArrayList pairs = new ArrayList<>(); for (Map.Entry param : params.entrySet()) { - try { - pairs.add(new BasicNameValuePair(param.getKey(), URLEncoder.encode(param.getValue(), "utf-8"))); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } + pairs.add(new BasicNameValuePair(param.getKey(), HttpUtil.encodeStr(param.getValue()))); } return pairs; } private static String covertParamsForStr(Map paraMap) { - if(paraMap == null){ + if (paraMap == null) { paraMap = new HashMap<>(); } - paraMap= new TreeMap<>(paraMap); + paraMap = new TreeMap<>(paraMap); StringBuilder sb = new StringBuilder(); - paraMap.entrySet().stream().forEach(entry ->{ - sb.append(entry.getKey()); + paraMap.forEach((key, value) -> { + sb.append(key); sb.append("="); - try { - sb.append(URLEncoder.encode(entry.getValue(),"utf-8")); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } + sb.append(HttpUtil.encodeStr(value)); sb.append("&"); }); return sb.toString(); @@ -198,15 +186,27 @@ private static String getResult(HttpRequestBase request) { response.close(); } catch (IOException e) { //e.printStackTrace(); - }finally { - try{ - if(httpClient != null){ + } finally { + try { + if (httpClient != null) { httpClient.close(); //释放资源 } - }catch (Exception e){ + } catch (Exception ignored) { } } return result; } + + + public static String encodeStr(String value) { + if (StrUtil.isBlank(value)) { + return Strings.EMPTY; + } + try { + return URLEncoder.encode(value, StandardCharsets.UTF_8); + } catch (Exception e) { + return Strings.EMPTY; + } + } } diff --git a/spring-boot-cps/cps-spring-boot-autoconfigure/src/main/java/in/hocg/boot/cps/autoconfiguration/impl/dataoke/lib/HttpUtils.java b/spring-boot-cps/cps-spring-boot-autoconfigure/src/main/java/in/hocg/boot/cps/autoconfiguration/impl/dataoke/lib/HttpUtils.java index 8cab0113..48035f22 100644 --- a/spring-boot-cps/cps-spring-boot-autoconfigure/src/main/java/in/hocg/boot/cps/autoconfiguration/impl/dataoke/lib/HttpUtils.java +++ b/spring-boot-cps/cps-spring-boot-autoconfigure/src/main/java/in/hocg/boot/cps/autoconfiguration/impl/dataoke/lib/HttpUtils.java @@ -8,8 +8,6 @@ import org.apache.http.util.EntityUtils; import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; @@ -20,6 +18,7 @@ * @createTime: 2019/04/24 14:55 * @description: */ +@Deprecated public class HttpUtils { public static String doGet(String url) { @@ -56,14 +55,10 @@ public static String sendGet(String getUrl, Map paraMap) throws } paraMap = new TreeMap<>(paraMap); StringBuilder sb = new StringBuilder(); - paraMap.entrySet().stream().forEach(entry -> { - sb.append(entry.getKey()); + paraMap.forEach((key, value) -> { + sb.append(key); sb.append("="); - try { - sb.append(URLEncoder.encode(entry.getValue(), "utf-8")); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } + sb.append(HttpUtil.encodeStr(value)); sb.append("&"); }); getUrl = getUrl.contains("?") ? getUrl : getUrl + "?"; diff --git a/spring-boot-cps/cps-spring-boot-autoconfigure/src/main/java/in/hocg/boot/cps/autoconfiguration/impl/dataoke/lib/SignMD5Util.java b/spring-boot-cps/cps-spring-boot-autoconfigure/src/main/java/in/hocg/boot/cps/autoconfiguration/impl/dataoke/lib/SignMD5Util.java index 67427aad..81e4b07f 100644 --- a/spring-boot-cps/cps-spring-boot-autoconfigure/src/main/java/in/hocg/boot/cps/autoconfiguration/impl/dataoke/lib/SignMD5Util.java +++ b/spring-boot-cps/cps-spring-boot-autoconfigure/src/main/java/in/hocg/boot/cps/autoconfiguration/impl/dataoke/lib/SignMD5Util.java @@ -1,5 +1,6 @@ package in.hocg.boot.cps.autoconfiguration.impl.dataoke.lib; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.util.Iterator; import java.util.Set; @@ -14,13 +15,14 @@ public class SignMD5Util { /** * 获取签名的util - * @param map 请求参数 + * + * @param map 请求参数 * @param secretKey 密钥 * @return */ - public static String getSignStr(TreeMap map, String secretKey){ + public static String getSignStr(TreeMap map, String secretKey) { - if (map.size() == 0 ){ + if (map.size() == 0) { return ""; } @@ -30,7 +32,7 @@ public static String getSignStr(TreeMap map, String secretKey){ Iterator iter = keySet.iterator(); while (iter.hasNext()) { String key = iter.next(); - sb.append("&"+ key + "=" + map.get(key)); + sb.append("&" + key + "=" + map.get(key)); } sb.deleteCharAt(0); return sign(sb.toString(), secretKey); @@ -38,13 +40,14 @@ public static String getSignStr(TreeMap map, String secretKey){ /** * 获取签名的util - * @param map 请求参数 + * + * @param map 请求参数 * @param secretKey 密钥 * @return */ - public static String getSignStrNew(TreeMap map, String secretKey){ + public static String getSignStrNew(TreeMap map, String secretKey) { - if (map.size() == 0 ){ + if (map.size() == 0) { return ""; } StringBuilder sb = new StringBuilder(); @@ -58,8 +61,7 @@ public static String getSignStrNew(TreeMap map, String secretKey) return sign(sb.toString(), secretKey); } - public static String sign(String content, String key) - { + public static String sign(String content, String key) { String signStr = ""; signStr = content + "&key=" + key; //MD5加密后,字符串所有字符转换为大写 @@ -67,7 +69,6 @@ public static String sign(String content, String key) } /** - * * MD5加密算法 * * @param s @@ -77,15 +78,14 @@ public static String sign(String content, String key) public final static String MD5(String s) { char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; try { - byte[] btInput = s.getBytes("utf-8"); + byte[] btInput = s.getBytes(StandardCharsets.UTF_8); MessageDigest mdInst = MessageDigest.getInstance("MD5"); mdInst.update(btInput); byte[] md = mdInst.digest(); int j = md.length; char str[] = new char[j * 2]; int k = 0; - for (int i = 0; i < j; i++) - { + for (int i = 0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf];