|
@@ -0,0 +1,257 @@
|
|
|
+package com.tofly.scada.util;
|
|
|
+
|
|
|
+import cn.hutool.http.ContentType;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.tofly.common.core.entity.ResultRespone;
|
|
|
+import com.tofly.scada.common.Constant;
|
|
|
+import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
|
|
+import org.springframework.util.DigestUtils;
|
|
|
+
|
|
|
+
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.math.BigInteger;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+
|
|
|
+ * http工具类
|
|
|
+ *
|
|
|
+ * @author zev
|
|
|
+ * 2022/01/07
|
|
|
+ * <p>
|
|
|
+ * HttpClientUtil
|
|
|
+ */
|
|
|
+public class HttpClientUtil {
|
|
|
+
|
|
|
+
|
|
|
+ * 编码格式,发送编码格式统一用UTF-8
|
|
|
+ */
|
|
|
+ private static final String ENCODING = "UTF-8";
|
|
|
+
|
|
|
+
|
|
|
+ * 设置连接超时时间,单位毫秒
|
|
|
+ */
|
|
|
+ private static final int CONNECT_TIMEOUT = 3 * 60 * 1000;
|
|
|
+
|
|
|
+
|
|
|
+ * 请求响应超时时间,单位毫秒
|
|
|
+ */
|
|
|
+ private static final int SOCKET_TIMEOUT = 3 * 60 * 1000;
|
|
|
+
|
|
|
+
|
|
|
+ * 发送get请求;带请求头和请求参数
|
|
|
+ */
|
|
|
+ public static ResultRespone<Object> doGet(String url, Map<String, String> headers, Map<String, Object> params, HttpServletResponse response) throws Exception {
|
|
|
+
|
|
|
+ HttpResponse execute = HttpRequest.get(url)
|
|
|
+ .addHeaders(headers)
|
|
|
+ .form(params)
|
|
|
+ .timeout(CONNECT_TIMEOUT)
|
|
|
+ .execute();
|
|
|
+
|
|
|
+
|
|
|
+ return getHttpClientResult(execute, response);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ResultRespone<Object> doGetJoinId(String url, Map<String, String> headers, Map<String, Object> params, HttpServletResponse response) throws Exception {
|
|
|
+
|
|
|
+ String join = url + Constant.SYSTEM_SEPARATOR + params.get("id");
|
|
|
+ HttpResponse execute = HttpRequest.get(join)
|
|
|
+ .addHeaders(headers)
|
|
|
+ .timeout(CONNECT_TIMEOUT)
|
|
|
+ .execute();
|
|
|
+
|
|
|
+
|
|
|
+ return getHttpClientResult(execute, response);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 发送post请求;带请求头和请求参数
|
|
|
+ */
|
|
|
+ public static ResultRespone<Object> doPost(String url, Map<String, String> headers, Map<String, Object> params) throws IOException {
|
|
|
+ HttpResponse execute = HttpRequest.post(url)
|
|
|
+ .addHeaders(headers)
|
|
|
+
|
|
|
+ .form(params)
|
|
|
+
|
|
|
+ .timeout(CONNECT_TIMEOUT)
|
|
|
+ .execute();
|
|
|
+
|
|
|
+
|
|
|
+ return getHttpClientResult(execute, null);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 发送post请求;json格式参数
|
|
|
+ */
|
|
|
+ public static String doPostJson(String url, Map<String, String> headers, String json) throws IOException {
|
|
|
+ HttpResponse execute = HttpRequest.post(url)
|
|
|
+ .addHeaders(headers)
|
|
|
+ .body(json)
|
|
|
+ .timeout(CONNECT_TIMEOUT)
|
|
|
+ .execute();
|
|
|
+
|
|
|
+
|
|
|
+ return execute.body();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 发送put请求;带请求头和请求参数
|
|
|
+ */
|
|
|
+ public static ResultRespone<Object> doPut(String url, Map<String, String> headers, Map<String, Object> params) throws IOException {
|
|
|
+ HttpResponse execute = HttpRequest.put(url)
|
|
|
+ .addHeaders(headers)
|
|
|
+
|
|
|
+ .form(params)
|
|
|
+
|
|
|
+ .timeout(CONNECT_TIMEOUT)
|
|
|
+ .execute();
|
|
|
+
|
|
|
+
|
|
|
+ return getHttpClientResult(execute, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ResultRespone<Object> doPutJson(String url, Map<String, String> headers, String json) throws IOException {
|
|
|
+ HttpResponse execute = HttpRequest.put(url)
|
|
|
+ .addHeaders(headers)
|
|
|
+ .body(json)
|
|
|
+ .timeout(CONNECT_TIMEOUT)
|
|
|
+ .execute();
|
|
|
+
|
|
|
+
|
|
|
+ return getHttpClientResult(execute, null);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 发送delete请求;带请求头
|
|
|
+ */
|
|
|
+ public static ResultRespone<Object> doDelete(String url, Map<String, String> headers, Map<String, Object> params) throws Exception {
|
|
|
+ HttpResponse execute = HttpRequest.delete(url)
|
|
|
+ .addHeaders(headers)
|
|
|
+
|
|
|
+ .form(params)
|
|
|
+
|
|
|
+ .timeout(CONNECT_TIMEOUT)
|
|
|
+ .execute();
|
|
|
+
|
|
|
+
|
|
|
+ return getHttpClientResult(execute, null);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 执行请求获取响应体并释放资源
|
|
|
+ */
|
|
|
+ private static ResultRespone<Object> getHttpClientResult(HttpResponse execute, HttpServletResponse response) throws IOException {
|
|
|
+ String body = execute.body();
|
|
|
+ System.out.println(body);
|
|
|
+ ResultRespone respone = JSON.parseObject(execute.body(), ResultRespone.class);
|
|
|
+ return respone;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * MD5加密之方法二
|
|
|
+ * @explain java实现
|
|
|
+ * @param str
|
|
|
+ * 待加密字符串
|
|
|
+ * @return 16进制加密字符串
|
|
|
+ */
|
|
|
+ public static String encrypt2ToMD5(String str) {
|
|
|
+
|
|
|
+ String hexStr = "";
|
|
|
+ try {
|
|
|
+
|
|
|
+ MessageDigest md5 = MessageDigest.getInstance("MD5");
|
|
|
+
|
|
|
+ byte[] digest = md5.digest(str.getBytes("utf-8"));
|
|
|
+ hexStr = new BigInteger(1, digest).toString(16);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return hexStr;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * @param text
|
|
|
+ * @return 传统的MD5加密方式,与客户端加密方法相同
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String TraditionMd5(String text) {
|
|
|
+
|
|
|
+ byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
|
|
|
+ byte[] encodeStrByte= DigestUtils.md5Digest(bytes);
|
|
|
+ String re=bytesToHexFun1(encodeStrByte);
|
|
|
+ return re;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5',
|
|
|
+ '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
|
|
+
|
|
|
+
|
|
|
+ * byte[]数组转16进制字符串
|
|
|
+ * @param bytes
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String bytesToHexFun1(byte[] bytes) {
|
|
|
+
|
|
|
+ char[] buf = new char[bytes.length * 2];
|
|
|
+ int a = 0;
|
|
|
+ int index = 0;
|
|
|
+ for(byte b : bytes) {
|
|
|
+ if(b < 0) {
|
|
|
+ a = 256 + b;
|
|
|
+ } else {
|
|
|
+ a = b;
|
|
|
+ }
|
|
|
+
|
|
|
+ buf[index++] = HEX_CHAR[a / 16];
|
|
|
+ buf[index++] = HEX_CHAR[a % 16];
|
|
|
+ }
|
|
|
+
|
|
|
+ return new String(buf);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|