xin
2025-06-04 061a01ce068bf8e1260b6ea8e5c610737185916a
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
package com.oying.utils;
 
import com.alibaba.fastjson2.JSONObject;
import com.oying.exception.BadRequestException;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
 
import javax.net.ssl.SSLContext;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * 功能描述:
 *
 * @author LIX
 * @date 创建时间 :2022/6/15 下午4:19
 */
public class HttpRequest {
 
 
    /**
     * 读取请求数据流
     *
     * @param request 请求数据
     * @return String
     */
    public static String getRequestBody(HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();
        try (ServletInputStream inputStream = request.getInputStream();
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            throw new BadRequestException("读取数据流异常");
        }
        return sb.toString();
    }
 
    /**
     * 忽略ssl证书验证
     *
     * @return HttpComponentsClientHttpRequestFactory
     */
    public static HttpComponentsClientHttpRequestFactory getFactory() {
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        try {
            TrustStrategy acceptingTrustStrategy = (chain, authType) -> true;
            SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
            SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
            HttpClientBuilder clientBuilder = HttpClients.custom();
            CloseableHttpClient httpClient = clientBuilder.setSSLSocketFactory(socketFactory).build();
            requestFactory.setHttpClient(httpClient);
        } catch (Exception e) {
            throw new BadRequestException("忽略ssl证书验证失败!!!");
        }
        return requestFactory;
    }
 
    private static HttpHeaders getHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        return headers;
    }
 
    private static HttpHeaders getMsg() {
        HttpHeaders headers = new HttpHeaders();
        List<MediaType> mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.ALL);
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); // 表单格式
        headers.setAccept(mediaTypes);
        headers.setConnection("Keep-Alive");
        headers.set("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        return headers;
    }
 
    /**
     * 短信
     * http返回String xml
     */
    public static String exchangeMsg(HttpMethod httpMethod, String url, MultiValueMap<String, String> map) {
        HttpHeaders headers = getMsg();
        HttpEntity<Object> httpEntity = new HttpEntity<>(map, headers);
        RestTemplate restTemplate = new RestTemplate(getFactory());
        return restTemplate.exchange(url, httpMethod, httpEntity, String.class).getBody();
    }
 
    /**
     * http返回String
     */
    public static String exchangeString(HttpMethod httpMethod, String url, Map<String, Object> map) {
        HttpHeaders headers = getHeaders();
        HttpEntity<Object> httpEntity = new HttpEntity<>(map, headers);
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.exchange(url, httpMethod, httpEntity, String.class).getBody();
    }
 
    /**
     * http返回JSONObject
     */
    public static JSONObject exchangeJsonObject(HttpMethod httpMethod, String url, Map<String, Object> map) {
        HttpHeaders headers = getHeaders();
        HttpEntity<Object> httpEntity = new HttpEntity<>(map, headers);
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.exchange(url, httpMethod, httpEntity, JSONObject.class).getBody();
    }
 
 
}