fwq
2024-09-27 1dd767dcc9919e3a0068835cd0dbca57f94debd5
提交 | 用户 | age
826b66 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;
9 import java.net.SocketTimeoutException;
10 import java.net.URL;
11 import java.util.Iterator;
12 import java.util.Map;
13
14 public class HttpHzUtil {
15
16     /**
17      * 请求方法
18      */
19     public static final String METHOD_POST = "POST";
20     /**
21      * 编码
22      */
23     public static final String CHARSET = "UTF-8";
24
25
26     /**
27      * 请求
28      *
29      * @param url           请求链接
30      * @param bodyData      请求body数据
31      * @param keyValues     连接携带参数
32      * @param header        表头携带参数
33      * @param requestMethod 请求类型,默认:POST
34      * @param outTime       超时时间(毫秒),默认:300000
35      * @return
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
47                 while (var9.hasNext()) {
48                     entry = (Map.Entry) var9.next();
49                     dataP.append((String) entry.getKey()).append("=");
50                     dataP.append(entry.getValue());
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
62             if (outTime == null || outTime < 0) {
63                 outTime = 300000;
64             }
65
66             URL httpUrl = new URL(url);
67             con = (HttpURLConnection) httpUrl.openConnection();
68             con.setRequestMethod(requestMethod);
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
78                 while (var9.hasNext()) {
79                     entry = (Map.Entry) var9.next();
80                     con.setRequestProperty((String) entry.getKey(), (String) entry.getValue());
81                 }
82             }
83
84             if (bodyData != null) {
85                 OutputStream os = con.getOutputStream();
86                 os.write(bodyData.getBytes(CHARSET));
87             }
88
89             httpHzResponse.setCode(con.getResponseCode() + "");
90             if (con.getErrorStream() != null) {
91                 httpHzResponse.setMsg(IOUtils.toString(con.getErrorStream(), CHARSET));
92             }
93             if (HttpURLConnection.HTTP_OK == con.getResponseCode() || HttpURLConnection.HTTP_CREATED == con.getResponseCode()) {
94                 httpHzResponse.setData(IOUtils.toString(con.getInputStream(), CHARSET));
95             }
96
97         }catch (SocketTimeoutException var15) {
98             httpHzResponse.setMsg(var15.getMessage());
99             httpHzResponse.setCode(HttpHzResponse.CODE_TIME_OUT);
100             var15.printStackTrace();
101         } catch (Exception var14) {
102             httpHzResponse.setMsg(var14.getMessage());
103             var14.printStackTrace();
104         } finally {
105             if (con != null) {
106                 con.disconnect();
107             }
108
109         }
110
111         return httpHzResponse;
112     }
113
114 }