jiangping
2024-01-16 c2f1aac8acca57f4c21f6fe6718101b01805bc72
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package com.jzq.common.http;
 
import com.alibaba.fastjson.JSONObject;
import com.jzq.common.CommonUtil;
import com.jzq.common.ResultInfo;
import com.jzq.common.exception.ResultInfoException;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.*;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.File;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.charset.CodingErrorAction;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * http的请求服务类
 * 也可以考虑使用spring restTemplate调用
 */
public class HttpClientUtils {
    public static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
    private static Object objTg = new Object();
    private static HttpClientUtils httpClientUtils;
    /**
     * 默认连接超时时间
     */
    private final static int DEFAULT_CONN_TIMEOUT = 6000;
    /**最大重试次数*/
    private final static int DEFAULT_RETRY_TIMES = 3;
 
    private CloseableHttpClient client;
 
    /**
     * ssl trust管理
     */
    public static class SSLTrustAllManager implements X509TrustManager {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    }
 
    /**
     * 初始化ssl管理
     * @return
     * @throws KeyStoreException
     * @throws CertificateException
     * @throws NoSuchAlgorithmException
     * @throws IOException
     * @throws KeyManagementException
     */
    private SSLConnectionSocketFactory initSSLConnectionSocketFactory() throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException, KeyManagementException {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        SSLContext sslcontext = SSLContexts.custom()
                .loadTrustMaterial(trustStore,
                        new TrustSelfSignedStrategy())
                .build();
        sslcontext.init(null, new TrustManager[] { new SSLTrustAllManager() }, null);
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslcontext,
                //new String[] { "TLSv1","TLSv1.1","TLSv1.2","SSLv3"},//参考ProtocolVersion//这里写为null为所有
                null,
                null,
                SSLConnectionSocketFactory.getDefaultHostnameVerifier());
        return sslsf;
    }
 
 
    public HttpClientUtils() throws Exception {
        // 设置协议http和https对应的处理socket链接工厂的对象
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.INSTANCE)
                .register("https", initSSLConnectionSocketFactory())
                .build();
        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        connManager.setValidateAfterInactivity(1000);
        ConnectionConfig connectionConfig = ConnectionConfig.custom()
                .setMalformedInputAction(CodingErrorAction.IGNORE)
                .setUnmappableInputAction(CodingErrorAction.IGNORE)
                .setCharset(Consts.UTF_8)
                .build();
        connManager.setDefaultConnectionConfig(connectionConfig);
        connManager.setMaxTotal(100);
        connManager.setDefaultMaxPerRoute(10);//最大路由深度,即301次数
 
        //默认头信息,创建自定义的httpclient对象
        List<Header> defaultHeaders=new ArrayList<Header>();
        defaultHeaders.add(new BasicHeader("Accept","text/html,application/xhtml+xml,application/xml,application/json;q=0.9,*/*;q=0.8"));
        defaultHeaders.add(new BasicHeader("Accept-Language","zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2"));
        defaultHeaders.add(new BasicHeader("Connection","close"));
        defaultHeaders.add(new BasicHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0"));
        //默认请求配置
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(DEFAULT_CONN_TIMEOUT)
                .setConnectTimeout(DEFAULT_CONN_TIMEOUT)
                //.setProxy(new HttpHost("myotherproxy", 8080)) //设置代理
                .setConnectionRequestTimeout(DEFAULT_CONN_TIMEOUT).build();
        //重试拦截
        HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
            @Override
            public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
                if (executionCount >= DEFAULT_RETRY_TIMES) {
                    //Do not retry if over max retry count
                    return false;
                }
                if (exception instanceof InterruptedIOException) {
                    // Timeout
                    return false;
                }
                if (exception instanceof UnknownHostException) {
                    // Unknown host
                    return false;
                }
                if (exception instanceof ConnectException) {
                    // Connection refused
                    return false;
                }
                if (exception instanceof SSLException) {
                    // SSL handshake exception
                    return false;
                }
                HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
                boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
                if (idempotent) {
                    return true;
                }
                return false;
            }
        };
 
        client = HttpClients.custom()
                .setConnectionManager(connManager)
                .setDefaultHeaders(defaultHeaders)
                .setDefaultRequestConfig(requestConfig)
                .setRetryHandler(myRetryHandler)
                .setRedirectStrategy(new LaxRedirectStrategy())
                .build();
    }
    /**
     * init实例
     * @return
     */
    public static HttpClientUtils init(){
        synchronized (objTg) {
            if (httpClientUtils == null) {
                try {
                    httpClientUtils = new HttpClientUtils();
                } catch (Exception e) {
                    throw new ResultInfoException("ACCESS_SIGN_ERROR","httpClient初始化出错",e);
                }
            }
        }
        return httpClientUtils;
    }
 
 
    /**
     * 工具类,通过url和params 生成url地址
     * @param url
     * @param params
     * @return
     * @throws URISyntaxException
     */
    public static URI builderUrl(String url, Map<String, Object> params){
        URIBuilder builder = null;
        try {
            builder = new URIBuilder(url);
            if(params!=null&&params.size()>0){
                for(String key: params.keySet()){
                    Object obj=params.get(key);
                    if(obj==null){continue;}
                    builder.setParameter(key, CommonUtil.parValNoErr(obj, String.class));
                }
            }
            return builder.build();
        } catch (URISyntaxException e) {
           throw new ResultInfoException("HTTP_URL_FORMART","转换地址出错:"+url,e);
        }
    }
 
    public static void fillHeader(HttpRequestBase request, Map<String, Object> heads){
        //请求头
        if (heads != null && heads.size() > 0) {
            for (String key : heads.keySet()) {
                request.addHeader(key, heads.get(key) + "");
            }
        }
    }
    /**
     * get请求
     * @param uri
     * @param heads
     * @param params
     * @return
     * @throws Exception
     */
    public String getGet(String uri, Map<String, Object> heads, Map<String, Object> params) {
        HttpGet request = new HttpGet(builderUrl(uri,params));
        fillHeader(request,heads);
        CloseableHttpResponse response = null;
        try {
            response = client.execute(request);
            if (response.getStatusLine().getStatusCode() == 200) {
                // 获得返回的字符串
                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                return result;
            } else {
                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                result= StringUtils.isNotBlank(result)?result.substring(0,result.length()>200?200:result.length()) : "";
                ResultInfo<String> res=new ResultInfo<String>();
                res.setData(result);
                res.setSuccess(false);
                res.setResultCode(response.getStatusLine().getStatusCode()+"");
                throw new ResultInfoException("HTTP_RESPONSE_ERROR", JSONObject.toJSONString(res));
            }
        } catch (IOException e) {
            throw new ResultInfoException("EXCEPTION","网络请求失败",e);
        } finally {
            try {
                response.close();
            } catch (Exception e) {}
        }
    }
 
    public byte[] getGetByte(String uri, Map<String, Object> heads, Map<String, Object> params) {
        HttpGet request = new HttpGet(builderUrl(uri,params));
        fillHeader(request,heads);
        CloseableHttpResponse response = null;
        try {
            response = client.execute(request);
            if (response.getStatusLine().getStatusCode() == 200) {
                // 获得返回的字符串
                byte[] result = EntityUtils.toByteArray(response.getEntity());
                return result;
            } else {
                ResultInfo<String> res=new ResultInfo<String>();
                res.setSuccess(false);
                res.setResultCode(response.getStatusLine().getStatusCode()+"");
                throw new ResultInfoException("HTTP_RESPONSE_ERROR", JSONObject.toJSONString(res));
            }
        } catch (IOException e) {
            throw new ResultInfoException("EXCEPTION","网络请求失败",e);
        } finally {
            try {
                response.close();
            } catch (Exception e) {}
        }
    }
 
    /**
     * 直接获取返回体,注意要手动关闭response
     * @param uri
     * @param heads
     * @param params
     * @return
     */
    public CloseableHttpResponse getGetResponse(String uri, Map<String, Object> heads, Map<String, Object> params) throws IOException {
        HttpGet request = new HttpGet(builderUrl(uri,params));
        fillHeader(request,heads);
        CloseableHttpResponse response = null;
        response = client.execute(request);
        return response;
    }
 
    /**
     * 构建post的body信息,
     * @param request
     * @param params
     * @param ifMutipart 是否启用富文件方式上传
     */
    private void buildPostBody(HttpPost request, Map<String, Object> params, boolean ifMutipart){
        if(ifMutipart){
            //富文本请求
            MultipartEntityBuilder meBuiler = MultipartEntityBuilder.create();
            if (params != null && params.size() > 0) {
                for (String key : params.keySet()) {
                    Object obj = params.get(key);
                    if(obj==null){
                        continue;
                    }else if (obj instanceof File) {
                        FileBody fb = new FileBody((File) obj);
                        meBuiler.addPart(key, fb);
                    } else if (obj instanceof ByteArrayBody) {
                        meBuiler.addPart(key, (ByteArrayBody) obj);
                    } else if (obj instanceof FileBody) {
                        meBuiler.addPart(key, (FileBody) obj);
                    } else if (obj instanceof InputStreamBody) {
                        meBuiler.addPart(key, (InputStreamBody) obj);
                    }else {
                        StringBody sb = new StringBody(CommonUtil.parValNoErrDef(obj, String.class, ""), ContentType.create("text/plain", "UTF-8"));
                        meBuiler.addPart(key, sb);
                    }
                }
            }
            HttpEntity httpEntity = meBuiler.build();
            request.setEntity(httpEntity);
        }else{
            //普通请求
            List<NameValuePair> pList = new ArrayList<NameValuePair>();
            if (params != null && params.size() > 0) {
                for (String key : params.keySet()) {
                    Object obj=params.get(key);
                    if(obj==null){continue;}
                    pList.add(new BasicNameValuePair(key, obj + ""));
                }
            }
            try {
                request.setEntity(new UrlEncodedFormEntity(pList, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                throw new ResultInfoException("HTTP_REQ_ENCODE_ERROR","请求参数格式化出错",e);
            }
        }
    }
 
    /**
     * post的上传请求
     * @param uri
     * @param heads
     * @param params
     * @return
     * @throws Exception
     */
    public String getPost(String uri, Map<String, Object> heads, Map<String, Object> params, boolean ifMutipart) {
        HttpPost request = new HttpPost(uri);
        fillHeader(request,heads);
        buildPostBody(request,params,ifMutipart);
        CloseableHttpResponse response = null;
        try {
            response = client.execute(request);
            if (response.getStatusLine().getStatusCode() == 200) {
                // 获得返回的字符串
                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                return result;
            } else {
                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                result= StringUtils.isNotBlank(result) ?result.substring(0,result.length()>200?200:result.length()) : "";
                ResultInfo<String> res=new ResultInfo<String>();
                res.setData(result);
                res.setSuccess(false);
                res.setResultCode(response.getStatusLine().getStatusCode()+"");
                throw new ResultInfoException("HTTP_RESPONSE_ERROR", JSONObject.toJSONString(res));
            }
        } catch (IOException e) {
            throw new ResultInfoException("HTTP_IO_ERROR","网络请求失败",e);
        } finally {
            try {
                response.close();
            } catch (Exception e) {}
        }
    }
 
    /**
     * post的上传请求,返回byte[]
     * @param uri
     * @param heads
     * @param params
     * @return
     * @throws Exception
     */
    public byte[] getPostByte(String uri, Map<String, Object> heads, Map<String, Object> params, boolean ifMutipart) {
        HttpPost request = new HttpPost(uri);
        fillHeader(request,heads);
        buildPostBody(request,params,ifMutipart);
        CloseableHttpResponse response = null;
        try {
            response = client.execute(request);
            if (response.getStatusLine().getStatusCode() == 200) {
                // 获得返回的byte[]
                byte[] result = EntityUtils.toByteArray(response.getEntity());
                return result;
            } else {
                ResultInfo<String> res=new ResultInfo<String>();
                res.setSuccess(false);
                res.setResultCode(response.getStatusLine().getStatusCode()+"");
                throw new ResultInfoException("HTTP_RESPONSE_ERROR", JSONObject.toJSONString(res));
            }
        } catch (IOException e) {
            throw new ResultInfoException("HTTP_IO_ERROR","网络请求失败",e);
        } finally {
            try {
                response.close();
            } catch (Exception e) {}
        }
    }
 
    /**
     * post的上传请求,返回CloseableHttpResponse,必须手动关闭
     * @param uri
     * @param heads
     * @param params
     * @return
     * @throws Exception
     */
    public CloseableHttpResponse getPostResponse(String uri, Map<String, Object> heads, Map<String, Object> params, boolean ifMutipart) {
        HttpPost request = new HttpPost(uri);
        fillHeader(request,heads);
        buildPostBody(request,params,ifMutipart);
        CloseableHttpResponse response = null;
        try {
            response = client.execute(request);
            if (response.getStatusLine().getStatusCode() == 200) {
                return response;
            } else {
                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                result= StringUtils.isNotBlank(result)?result.substring(0,result.length()>200?200:result.length()) : "";
                ResultInfo<String> res=new ResultInfo<String>();
                res.setData(result);
                res.setSuccess(false);
                res.setResultCode(response.getStatusLine().getStatusCode()+"");
                throw new ResultInfoException("HTTP_RESPONSE_ERROR", JSONObject.toJSONString(res));
            }
        } catch (IOException e) {
            throw new ResultInfoException("HTTP_IO_ERROR","网络请求失败",e);
        } finally {
            try {
                response.close();
            } catch (Exception e) {}
        }
    }
}