chenjiahe
2022-12-16 5bfcd30741d716cb46bb4540051c3bed65737189
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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.SocketTimeoutException;
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() + "");
            if (con.getErrorStream() != null) {
                httpHzResponse.setMsg(IOUtils.toString(con.getErrorStream(), CHARSET));
            }
            if (HttpURLConnection.HTTP_OK == con.getResponseCode() || HttpURLConnection.HTTP_CREATED == con.getResponseCode()) {
                httpHzResponse.setData(IOUtils.toString(con.getInputStream(), CHARSET));
            }
 
        }catch (SocketTimeoutException var15) {
            httpHzResponse.setMsg(var15.getMessage());
            httpHzResponse.setCode(HttpHzResponse.CODE_TIME_OUT);
            var15.printStackTrace();
        } catch (Exception var14) {
            httpHzResponse.setMsg(var14.getMessage());
            var14.printStackTrace();
        } finally {
            if (con != null) {
                con.disconnect();
            }
 
        }
 
        return httpHzResponse;
    }
 
}