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