From b8f5f5e33a457c006fe8b18d8228e11ef025c229 Mon Sep 17 00:00:00 2001 From: chenjiahe <763432473@qq.com> Date: 星期四, 31 三月 2022 15:23:54 +0800 Subject: [PATCH] 新增请求方法 --- src/main/java/com/hx/util/rsa/RSAUtil.java | 1 src/main/java/com/hz/util/http/HttpHzUtil.java | 97 ++++++++++++++++++++++++++++++++ src/main/java/com/hz/util/http/dto/HttpHzResponse.java | 38 ++++++++++++ 3 files changed, 136 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/hx/util/rsa/RSAUtil.java b/src/main/java/com/hx/util/rsa/RSAUtil.java index 20bb3f0..f874515 100644 --- a/src/main/java/com/hx/util/rsa/RSAUtil.java +++ b/src/main/java/com/hx/util/rsa/RSAUtil.java @@ -64,6 +64,7 @@ byte[] bytes = cipher.doFinal(content.getBytes()); return byte2Base64(bytes); } catch (Exception e) { + e.printStackTrace(); //logger.error("鍏挜鍔犲瘑澶辫触{}", e); } return null; diff --git a/src/main/java/com/hz/util/http/HttpHzUtil.java b/src/main/java/com/hz/util/http/HttpHzUtil.java new file mode 100644 index 0000000..2c33ff4 --- /dev/null +++ b/src/main/java/com/hz/util/http/HttpHzUtil.java @@ -0,0 +1,97 @@ +package com.hz.util.http; + +import com.hx.util.StringUtils; +import com.hz.util.http.dto.HttpHzResponse; +import org.apache.commons.io.IOUtils; + +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Iterator; +import java.util.Map; + +public class HttpHzUtil { + + /**璇锋眰鏂规硶*/ + public static final String METHOD_POST = "POST"; + /**缂栫爜*/ + public static final String CHARSET = "UTF-8"; + + + /**璇锋眰 + * @param url 璇锋眰閾炬帴 + * @param bodyData 璇锋眰body鏁版嵁 + * @param keyValues 杩炴帴鎼哄甫鍙傛暟 + * @param header 琛ㄥご鎼哄甫鍙傛暟 + * @param requestMethod 璇锋眰绫诲瀷锛岄粯璁わ細POST + * @param outTime 瓒呮椂鏃堕棿锛堟绉掞級,榛樿锛�300000 + * @return + */ + public static HttpHzResponse HttpURLUtilJson(String url, String bodyData, Map<String, Object> keyValues, Map<String, String> header, String requestMethod, Integer outTime) { + HttpHzResponse httpHzResponse = new HttpHzResponse(); + HttpURLConnection con = null; + try { + StringBuilder dataP = new StringBuilder(); + Iterator var9; + Map.Entry entry; + if (keyValues != null && !keyValues.isEmpty()) { + var9 = keyValues.entrySet().iterator(); + + while(var9.hasNext()) { + entry = (Map.Entry)var9.next(); + dataP.append((String)entry.getKey()).append("="); + dataP.append(entry.getValue()); + dataP.append("&"); + } + + dataP.deleteCharAt(dataP.length() - 1); + url = url + "?" + dataP; + } + + if (StringUtils.isEmpty(requestMethod)) { + requestMethod = METHOD_POST; + } + + if(outTime == null || outTime < 0){ + outTime = 300000; + } + + URL httpUrl = new URL(url); + con = (HttpURLConnection)httpUrl.openConnection(); + con.setRequestMethod(requestMethod); + con.setDoOutput(true); + con.setDoInput(true); + con.setUseCaches(false); + con.setReadTimeout(outTime); + con.setRequestProperty("Charset", CHARSET); + con.setRequestProperty("Content-Type", "application/json"); + if (header != null) { + var9 = header.entrySet().iterator(); + + while(var9.hasNext()) { + entry = (Map.Entry)var9.next(); + con.setRequestProperty((String)entry.getKey(), (String)entry.getValue()); + } + } + + if (bodyData != null) { + OutputStream os = con.getOutputStream(); + os.write(bodyData.getBytes(CHARSET)); + } + + httpHzResponse.setCode(con.getResponseCode()+""); + httpHzResponse.setData(IOUtils.toString(con.getInputStream(), CHARSET)); + + } catch (Exception var14) { + var14.printStackTrace(); + } finally { + if (con != null) { + con.disconnect(); + } + + } + + return httpHzResponse; + } + +} diff --git a/src/main/java/com/hz/util/http/dto/HttpHzResponse.java b/src/main/java/com/hz/util/http/dto/HttpHzResponse.java new file mode 100644 index 0000000..0d492ce --- /dev/null +++ b/src/main/java/com/hz/util/http/dto/HttpHzResponse.java @@ -0,0 +1,38 @@ +package com.hz.util.http.dto; + +/** + * 璇锋眰杩斿洖缁熶竴dto + */ +public class HttpHzResponse { + + /**璇锋眰鐘舵��*/ + private String code; + /**閿欒淇℃伅*/ + private String msg; + /**杩斿洖淇℃伅*/ + private String data; + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } +} -- Gitblit v1.8.0