fwq
2022-12-26 528175df22e9c0b9b6d20d83468f2985463105f3
提交 | 用户 | age
b8f5f5 1 package com.hz.util.http;
C 2
3 import com.hx.util.StringUtils;
4 import com.hz.util.http.dto.HttpHzResponse;
5 import org.apache.commons.io.IOUtils;
6
7 import java.io.OutputStream;
8 import java.net.HttpURLConnection;
98ee04 9 import java.net.SocketTimeoutException;
b8f5f5 10 import java.net.URL;
C 11 import java.util.Iterator;
12 import java.util.Map;
13
14 public class HttpHzUtil {
15
98ee04 16     /**
F 17      * 请求方法
18      */
19     public static final String METHOD_POST = "POST";
20     /**
21      * 编码
22      */
23     public static final String CHARSET = "UTF-8";
b8f5f5 24
C 25
98ee04 26     /**
F 27      * 请求
28      *
29      * @param url           请求链接
30      * @param bodyData      请求body数据
31      * @param keyValues     连接携带参数
32      * @param header        表头携带参数
b8f5f5 33      * @param requestMethod 请求类型,默认:POST
98ee04 34      * @param outTime       超时时间(毫秒),默认:300000
b8f5f5 35      * @return
C 36      */
37     public static HttpHzResponse HttpURLUtilJson(String url, String bodyData, Map<String, Object> keyValues, Map<String, String> header, String requestMethod, Integer outTime) {
38         HttpHzResponse httpHzResponse = new HttpHzResponse();
39         HttpURLConnection con = null;
40         try {
41             StringBuilder dataP = new StringBuilder();
42             Iterator var9;
43             Map.Entry entry;
44             if (keyValues != null && !keyValues.isEmpty()) {
45                 var9 = keyValues.entrySet().iterator();
46
98ee04 47                 while (var9.hasNext()) {
F 48                     entry = (Map.Entry) var9.next();
49                     dataP.append((String) entry.getKey()).append("=");
b8f5f5 50                     dataP.append(entry.getValue());
C 51                     dataP.append("&");
52                 }
53
54                 dataP.deleteCharAt(dataP.length() - 1);
55                 url = url + "?" + dataP;
56             }
57
58             if (StringUtils.isEmpty(requestMethod)) {
59                 requestMethod = METHOD_POST;
60             }
61
98ee04 62             if (outTime == null || outTime < 0) {
b8f5f5 63                 outTime = 300000;
C 64             }
65
66             URL httpUrl = new URL(url);
98ee04 67             con = (HttpURLConnection) httpUrl.openConnection();
b8f5f5 68             con.setRequestMethod(requestMethod);
C 69             con.setDoOutput(true);
70             con.setDoInput(true);
71             con.setUseCaches(false);
72             con.setReadTimeout(outTime);
73             con.setRequestProperty("Charset", CHARSET);
74             con.setRequestProperty("Content-Type", "application/json");
75             if (header != null) {
76                 var9 = header.entrySet().iterator();
77
98ee04 78                 while (var9.hasNext()) {
F 79                     entry = (Map.Entry) var9.next();
80                     con.setRequestProperty((String) entry.getKey(), (String) entry.getValue());
b8f5f5 81                 }
C 82             }
83
84             if (bodyData != null) {
85                 OutputStream os = con.getOutputStream();
86                 os.write(bodyData.getBytes(CHARSET));
87             }
88
98ee04 89             httpHzResponse.setCode(con.getResponseCode() + "");
F 90             if (con.getErrorStream() != null) {
39f96c 91                 httpHzResponse.setMsg(IOUtils.toString(con.getErrorStream(), CHARSET));
C 92             }
98ee04 93             if (HttpURLConnection.HTTP_OK == con.getResponseCode() || HttpURLConnection.HTTP_CREATED == con.getResponseCode()) {
ebaba2 94                 httpHzResponse.setData(IOUtils.toString(con.getInputStream(), CHARSET));
C 95             }
b8f5f5 96
98ee04 97         }catch (SocketTimeoutException var15) {
F 98             httpHzResponse.setMsg(var15.getMessage());
99             httpHzResponse.setCode(HttpHzResponse.CODE_TIME_OUT);
100             var15.printStackTrace();
b8f5f5 101         } catch (Exception var14) {
98ee04 102             httpHzResponse.setMsg(var14.getMessage());
b8f5f5 103             var14.printStackTrace();
C 104         } finally {
105             if (con != null) {
106                 con.disconnect();
107             }
108
109         }
110
111         return httpHzResponse;
112     }
113
114 }