liusheng
2025-05-09 baed560033551773f244a1f16c81a4b5c9cca75c
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package com.ruoyi.common.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.util.StringUtil;
import com.ruoyi.common.exception.HttpRequestException;
import com.ruoyi.common.utils.http.HttpEntity;
import com.ruoyi.common.utils.json.JsonRequestBody;
import com.ruoyi.common.utils.json.JsonResponseBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.Charsets;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
 
@Slf4j
public class HttpUtil {
 
    protected static final int DEFAULT_STATUS_ERROR_CODE = 100001;//默认http异常状态码
 
    protected static final String CONTENT_TYPE = "Content-Type";
 
    protected static final String APPLICATION_JSON = "application/json";
    protected static final String APPLICATION_JSON_UTF8 = "application/json; charset=utf-8";
 
    protected static final String TEXT_XML = "text/xml";
    protected static final String TEXT_XML_UTF8 = "text/xml; charset=utf-8";
 
    protected static final int OK = 200;
 
 
    /**
     * 空的报文头
     */
    protected static final Map<String, String> EMPTY_HEADERS = new HashMap<String, String>(0);
    /**
     * 编码错误
     */
    protected static final int ENCODING_ERROR_CODE = 999997;
    /**
     * HTTP 错误: 死锁、文件过大等文件
     */
    protected static final int HTTP_ERROR_CODE = 999996;
    /**
     * IO 错误
     */
    protected static final int IO_ERROR_CODE = 999995;
    /**
     * 响应为null
     */
    protected static final int RESPONSE_NULL_ERROR_CODE = 999994;
 
    protected static final String USER_IP_KEY = "x-remoteip";
 
    protected static final String TEXT_NORMAL = "application/x-www-form-urlencoded; charset=utf-8";
    protected static final HttpClient httpClient = getHttpClient();
 
    private static final SimpleDateFormat formatHttpData = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
 
 
    private static HttpClient getHttpClient() {
        // 此处运用连接池技术。
        MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
 
        // 设定参数:与每个主机的最大连接数
        manager.getParams().setDefaultMaxConnectionsPerHost(100);
        // 设定参数:客户端的总连接数
        manager.getParams().setMaxTotalConnections(400);
        // 设置连接超时时间,单位:毫秒
        manager.getParams().setConnectionTimeout(30000);
        // 设置请求读取超时时间,单位:毫秒
        manager.getParams().setSoTimeout(30000);
        // 设置从连接池中获取链接时间, 单位:毫秒
        manager.getParams().setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, 8000);
        // 使用连接池技术创建HttpClient对象
        HttpClient httpClient = new HttpClient(manager);
 
        return httpClient;
    }
 
    public static String postJsonRequest(String url, String request) throws HttpRequestException {
        Assert.hasLength(url, "请求url不能为空字符串。");
        EntityEnclosingMethod httpMethod = new PostMethod(url);
 
        httpMethod.addRequestHeader("Content-Type", "application/json; charset=UTF-8");
        setHeaderRequestId(httpMethod);
        try {
            RequestEntity entity = new StringRequestEntity(request, "application/json", "utf-8");
            httpMethod.setRequestEntity(entity);
 
            int resultCode = httpClient.executeMethod(httpMethod);
            InputStream inputStream = httpMethod.getResponseBodyAsStream();
            if (inputStream == null) {
                throw new HttpRequestException(RESPONSE_NULL_ERROR_CODE, "响应为null");
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer = new StringBuffer();
            String str = "";
            while ((str = reader.readLine()) != null) {
                stringBuffer.append(str);
            }
            reader.close();
            String respJson = stringBuffer.toString();
            // String respJson = httpMethod.getResponseBodyAsString();
            if (resultCode == OK) {
                return respJson;
            } else {
                throw new HttpRequestException(resultCode, respJson);
            }
        } catch (UnsupportedEncodingException e) {
            throw new HttpRequestException(ENCODING_ERROR_CODE, e);
        } catch (HttpException e) {
            throw new HttpRequestException(HTTP_ERROR_CODE, e);
        } catch (IOException e) {
            throw new HttpRequestException(IO_ERROR_CODE, e);
        } finally {
            if (httpMethod != null) {
                httpMethod.releaseConnection();
            }
        }
    }
 
    /**
     * json的post请求
     *
     * @param url           请求url
     * @param reqEntity     请求头与请求体的封装
     * @param respBodyClass 响应体类型
     * @return 返回的响应结果
     */
    @SuppressWarnings("unchecked")
    public static <RESPBODY> HttpEntity<RESPBODY> postJsonRequestV2(String url, HttpEntity<?> reqEntity, Class<RESPBODY> respBodyClass) {
        Assert.hasLength(url, "请求url不能为空字符串。");
        Assert.notNull(reqEntity, "请求request不能为null。");
        Assert.notNull(reqEntity.getBody(), "Post请求body不能为null。");
 
        EntityEnclosingMethod httpMethod = new PostMethod(url);
        //设置header信息
        Map<String, String> headers = reqEntity.getHeaders();
        //若传入报文头,则设置值
        if (headers != HttpEntity.EMPTY_HEADERS) {
            setReqHeaders(headers, httpMethod);
        }
 
        //设置body信息
        String reqJson = JSON.toJSONString(reqEntity.getBody());
 
        String charset = com.google.common.base.Charsets.UTF_8.name();
        // 发送含xml消息体的对象
        try {
            RequestEntity entity = new StringRequestEntity(reqJson, APPLICATION_JSON, charset);
            httpMethod.setRequestEntity(entity);
 
            // 执行请求并接收响应码
            int resultCode = httpClient.executeMethod(httpMethod);
            InputStream inputStream = httpMethod.getResponseBodyAsStream();
            if (inputStream == null) {
                throw new HttpRequestException(RESPONSE_NULL_ERROR_CODE, "响应为null");
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("GB2312")));
            StringBuffer stringBuffer = new StringBuffer();
            String str = "";
            while ((str = reader.readLine()) != null) {
                stringBuffer.append(str);
            }
            reader.close();
            String respStr = stringBuffer.toString();
            //String respStr = httpMethod.getResponseBodyAsString();
            if (resultCode == OK) {
                //响应头
                Map<String, String> respHeaders = getRespHeaders(httpMethod);
                //响应体
                HttpEntity<RESPBODY> rep = null;
                if (isNullOrEmpty(respStr) || null == respBodyClass) {
                    rep = new HttpEntity<RESPBODY>(respHeaders, null);
                    // 无响应
                } else {
                    if (respBodyClass != String.class) {
                        RESPBODY respBody = JSON.parseObject(respStr, respBodyClass);
                        rep = new HttpEntity<RESPBODY>(respHeaders, respBody);
                    } else {
                        rep = new HttpEntity<RESPBODY>(respHeaders, (RESPBODY) new String(respStr.getBytes("ISO8859-1"), StandardCharsets.UTF_8));
                    }
                }
                return rep;
            } else if (resultCode == DEFAULT_STATUS_ERROR_CODE) {
                JSONObject model = JSON.parseObject(respStr);
                throw new HttpRequestException(model.getIntValue("code"), model.getString("msg"));
            } else {
                throw new HttpRequestException(resultCode, respStr);
            }
        } catch (UnsupportedEncodingException e) {
            throw new HttpRequestException(ENCODING_ERROR_CODE, e);
        } catch (HttpException e) {
            throw new HttpRequestException(HTTP_ERROR_CODE, e);
        } catch (IOException e) {
            throw new HttpRequestException(IO_ERROR_CODE, e);
        } finally {
            if (httpMethod != null) {
                httpMethod.releaseConnection();
            }
        }
    }
 
    public static <RESPBODY extends JsonResponseBody> HttpEntity<RESPBODY> postJsonRequest(String url, HttpEntity<? extends JsonRequestBody> reqEntity, Class<RESPBODY> respBodyClass) {
        Assert.hasLength(url, "请求url不能为空字符串。");
        Assert.notNull(reqEntity, "请求request不能为null。");
        Assert.notNull(reqEntity.getBody(), "Post请求body不能为null。");
 
        EntityEnclosingMethod httpMethod = new PostMethod(url);
        //设置header信息
        Map<String, String> headers = reqEntity.getHeaders();
        //若传入报文头,则设置值
        if (headers != HttpEntity.EMPTY_HEADERS) {
            setReqHeaders(headers, httpMethod);
        }
 
        //设置body信息
        String reqJson = JSON.toJSONString(reqEntity.getBody());
 
        String charset = Charsets.UTF_8.name();
        // 发送含xml消息体的对象
        try {
            RequestEntity entity = new StringRequestEntity(reqJson, APPLICATION_JSON, charset);
            httpMethod.setRequestEntity(entity);
 
            // 执行请求并接收响应码
            int resultCode = httpClient.executeMethod(httpMethod);
            InputStream inputStream = httpMethod.getResponseBodyAsStream();
            if (inputStream == null) {
                throw new HttpRequestException(RESPONSE_NULL_ERROR_CODE, "响应为null");
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer = new StringBuffer();
            String str = "";
            while ((str = reader.readLine()) != null) {
                stringBuffer.append(str);
            }
            reader.close();
            String respStr = stringBuffer.toString();
            if (resultCode == OK) {
                //响应头
                Map<String, String> respHeaders = getRespHeaders(httpMethod);
                //响应体
                HttpEntity<RESPBODY> rep = null;
                if (isNullOrEmpty(respStr) || null == respBodyClass) {
                    rep = new HttpEntity<RESPBODY>(respHeaders, null);
                    ;// 无响应
                } else {
                    RESPBODY respBody = JSON.parseObject(respStr, respBodyClass);
                    rep = new HttpEntity<RESPBODY>(respHeaders, respBody);
                }
                return rep;
            } else if (resultCode == DEFAULT_STATUS_ERROR_CODE) {
                JSONObject model = JSON.parseObject(respStr);
                throw new HttpRequestException(model.getIntValue("code"), model.getString("msg"));
            } else {
                throw new HttpRequestException(resultCode, respStr);
            }
        } catch (UnsupportedEncodingException e) {
            throw new HttpRequestException(ENCODING_ERROR_CODE, e);
        } catch (HttpException e) {
            throw new HttpRequestException(HTTP_ERROR_CODE, e);
        } catch (IOException e) {
            throw new HttpRequestException(IO_ERROR_CODE, e);
        } finally {
            if (httpMethod != null) {
                httpMethod.releaseConnection();
            }
        }
    }
 
    public static String postFormRequest(String url, Map<String, String> params) throws HttpRequestException {
        Assert.hasLength(url, "请求url不能为空字符串。");
        Assert.notNull(params, "请求params不能为空。");
 
        PostMethod httpMethod = new PostMethod(url);
 
        httpMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
 
        try {
            // 发送请求参数
            StringBuilder param = new StringBuilder();
            for (Map.Entry<String, String> entry : params.entrySet()) {
                if (param.length() > 0) {
                    param.append("&");
                }
                param.append(entry.getKey());
                param.append("=");
                param.append(entry.getValue());
            }
 
            RequestEntity entity = new StringRequestEntity(param.toString(), "application/json", "utf-8");
            httpMethod.setRequestEntity(entity);
            // 执行请求并接收响应码
            int resultCode = httpClient.executeMethod(httpMethod);
 
            String respJson = httpMethod.getResponseBodyAsString();
            if (resultCode == OK) {
                return respJson;
            } else {
                throw new HttpRequestException(resultCode, respJson);
            }
        } catch (UnsupportedEncodingException e) {
            throw new HttpRequestException(ENCODING_ERROR_CODE, e);
        } catch (HttpException e) {
            throw new HttpRequestException(HTTP_ERROR_CODE, e);
        } catch (IOException e) {
            throw new HttpRequestException(IO_ERROR_CODE, e);
        } finally {
            if (httpMethod != null) {
                httpMethod.releaseConnection();
            }
        }
    }
 
 
    private static Map<String, String> getRespHeaders(HttpMethodBase httpMethod) {
        //得到响应头
        Header[] respHeaders = httpMethod.getResponseHeaders();
        Map<String, String> headers = new HashMap<String, String>(respHeaders.length);
        for (Header header : respHeaders)
            headers.put(header.getName(), header.getValue());
        return headers;
    }
 
    protected static void setReqHeaders(Map<String, String> headers, HttpMethodBase httpMethod) {
        //设置请求头
        for (Map.Entry<String, String> header : headers.entrySet()) {
            httpMethod.addRequestHeader(header.getKey(), header.getValue());
        }
    }
 
    protected static void setHeaderRequestId(HttpMethodBase httpMethod) {
        //设置请求头
        ServletRequestAttributes req = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (null != req) {
            String requestId = req.getRequest().getHeader("x-request-id");
            if (StringUtil.isNotEmpty(requestId)) {
                httpMethod.addRequestHeader("x-request-id", requestId);
                log.info("header中x-request-id值为:{}", requestId);
            }
        }
    }
 
    private static boolean isNullOrEmpty(String obj) {
        if (obj == null || obj.isEmpty()) {
            return true;
        }
        return false;
    }
}