k94314517
2024-05-21 12e8eb9e345adea19972c34df471b28b51fa71dd
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package com.doumee.core.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
 
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.zip.GZIPInputStream;
 
/**
 * Http工具类
 * @author Eva.Caesar Liu
 * @date 2023/03/21 14:49
 */
@Slf4j
public class Http {
 
    /**
     * 获取Http连接
     * @param url 请求地址
     *
     * @return HttpWrap
     */
    public HttpWrap build(String url) throws IOException {
        return this.build(url, "UTF-8");
    }
 
    /**
     * 获取Http连接
     * @param url 请求地址
     * @param charset 编码,默认UTF-8
     *
     * @return HttpWrap
     */
    public HttpWrap build(String url, String charset) throws IOException {
        if (url == null) {
            throw new NullPointerException("url can not be null");
        }
        URL urlObj = new URL(url);
        HttpURLConnection httpURLConnection = (HttpURLConnection) urlObj.openConnection();
        httpURLConnection.setRequestProperty("ContentType", charset);
        return new HttpWrap(url, charset, httpURLConnection);
    }
 
    @Getter
    public static class HttpWrap {
 
        private String url;
 
        private String charset;
 
        private HttpURLConnection connection;
 
        private boolean gzip;
 
        public HttpWrap (String url, String charset, HttpURLConnection connection) {
            this.url = url;
            this.charset = charset;
            this.connection = connection;
        }
 
        /**
         * 开启GZIP压缩
         */
        public HttpWrap gzip () {
            this.gzip = Boolean.TRUE;
            return this;
        }
 
        /**
         * 设置请求属性
         * @param key 属性
         * @param value 属性值
         *
         * @return HttpWrap
         */
        public HttpWrap setRequestProperty (String key, String value) {
            connection.setRequestProperty(key, value);
            return this;
        }
 
        /**
         * 设置连接超时时间
         * @param timeout 连接超时时间
         *
         * @return HttpWrap
         */
        public HttpWrap setConnectTimeout (int timeout) {
            connection.setConnectTimeout(timeout);
            return this;
        }
 
        /**
         * 设置读取超时时间
         * @param timeout 读取超时时间
         *
         * @return HttpWrap
         */
        public HttpWrap setReadTimeout (int timeout) {
            connection.setReadTimeout(timeout);
            return this;
        }
 
        /**
         * 发送GET请求
         *
         * @return HttpResult
         */
        public HttpResult get () throws IOException {
            log.trace("Eva::Util::Http send http request by method GET, url=" + url);
            connection.setRequestMethod(HttpMethod.GET.toString());
            return new HttpResult(connection.getInputStream(), charset, gzip);
        }
 
        /**
         * 发送POST请求
         *
         * @return HttpResult
         */
        public HttpResult post () throws IOException {
            log.trace("Eva::Util::Http send http request by method POST, url=" + url);
            connection.setRequestMethod(HttpMethod.POST.toString());
            return new HttpResult(connection.getInputStream(), charset, gzip);
        }
 
        /**
         * 发送POST请求
         * @param params 请求参数
         *
         * @return HttpResult
         */
        public HttpResult post (String params) throws IOException {
            log.trace("Eva::Util::Http send http request by method POST, url=" + url);
            OutputStreamWriter streamWriter = null;
            try {
                connection.setRequestMethod(HttpMethod.POST.toString());
                if (params != null || "".equals(params.trim())) {
                    connection.setDoInput(Boolean.TRUE);
                    connection.setDoOutput(Boolean.TRUE);
                    streamWriter = new OutputStreamWriter(connection.getOutputStream(), charset);
                    streamWriter.write(params);
                    streamWriter.flush();
                }
                return new HttpResult(connection.getInputStream(), charset, gzip);
            } finally {
                if (streamWriter != null) {
                    streamWriter.close();
                }
            }
        }
 
        /**
         * 发送POST请求,请求参数类型为JSON
         * @param paramsMap 请求参数
         *
         * @return HttpResult
         */
        public HttpResult postJSON(Map<String, Object> paramsMap) throws IOException {
            setRequestProperty("Content-Type", "application/json");
            if (paramsMap == null || paramsMap.size() == 0) {
                return post();
            }
            return post(JSON.toJSONString(paramsMap));
        }
 
        /**
         * 发送POST请求,请求参数类型为JSON
         * @param paramJSONObject 请求参数
         *
         * @return HttpResult
         */
        public HttpResult postJSON(JSONObject paramJSONObject) throws IOException {
            setRequestProperty("Content-Type", "application/json");
            if (paramJSONObject == null || paramJSONObject.size() == 0) {
                return post();
            }
            return post(paramJSONObject.toJSONString());
        }
 
    }
 
    @Getter
    @AllArgsConstructor
    public static class HttpResult {
 
        private InputStream inputStream;
 
        private String charset;
 
        private boolean gzip;
 
        /**
         * 转为字符串
         */
        public String toStringResult () throws IOException{
            BufferedReader reader = null;
            InputStream is = inputStream;
            try {
                if (gzip) {
                    is = new GZIPInputStream(inputStream);
                }
                reader = new BufferedReader(new InputStreamReader(is, charset));
                StringBuilder result = new StringBuilder();
                String line;
                while((line = reader.readLine()) != null) {
                    result.append(line);
                }
                return result.toString();
            } finally {
                if (reader != null) {
                    reader.close();
                }
                if (is != null) {
                    is.close();
                }
            }
        }
 
        /**
         * 转为JSONObject对象
         */
        public JSONObject toJSONObject () throws IOException {
            return JSONObject.parseObject(toStringResult());
        }
 
        /**
         * 转为目标Class对象
         */
        public <T> T toClass (Class<T> clazz) throws IOException {
            return JSONObject.parseObject(toStringResult(), clazz);
        }
    }
}