zxl
6 小时以前 3b0516a2959e25576e4f3fda697a3b025d06c8c9
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
package com.ycl.utils.http;
 
import lombok.RequiredArgsConstructor;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
 
import java.util.Collections;
import java.util.Map;
 
/**
 * @author xp
 * @date 2022/11/16
 */
@Component
@RequiredArgsConstructor
public class SelfHttpUtil {
 
    private final RestTemplate restTemplate;
 
 
    /**
     * post
     *
     * @param url    请求连接(ps: post请求如果要传url参数,请先拼接好再传入url)
     * @param data   请求体数据
     * @param header 请求头(有token就放里面)
     * @return
     */
    public Object post(String url, @Nullable Object data, @Nullable MultiValueMap header) {
        ResponseEntity<Object> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                getHttpEntity(data, header),
                Object.class
        );
        return response.getBody();
    }
 
    /**
     * @param url   请求连接 (ps: post请求如果要传url参数,请先拼接好再传入url)
     * @param data  请求体数据
     * @param token
     * @return
     */
    public Object post(String url, String token, @Nullable Object data) {
        ResponseEntity<Object> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                getHttpEntity(data, token),
                Object.class
        );
        return response.getBody();
    }
 
    /**
     * get
     *
     * @param url  RestTemplate 如果有url参数,必须要在url中使用占位符,如:https://www.easyblog.vip?name={name} 传参:String name = xp,才能拼上这个参数
     * @param token
     * @param params
     * @return
     */
//    public Object get(String url, String token, @Nullable Object... params) {
//        ResponseEntity<Object> response = httpClient.exchange(
//                url,
//                HttpMethod.GET,
//                getHttpEntity(null, token),
//                Object.class,
//                params
//        );
//        return response.getBody();
//    }
 
    /**
     * get
     *
     * @param url    请求连接  RestTemplate 如果有url参数,必须要在url中使用占位符,如:https://www.easyblog.vip?name={name} 传参:String name = xp,才能拼上这个参数
     * @param header 请求头(有token就放里面)
     * @param params
     * @return
     */
    public ResponseEntity<String> get(String url, @Nullable MultiValueMap header, @Nullable Map<String, Object> params) {
        if (params == null) {
            params = Collections.emptyMap();
        }
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                getHttpEntity(null, header),
                String.class,
                params
        );
        return response;
    }
 
 
    /**
     * 构建带请求头和请求体的httpEntity
     *
     * @param data
     * @param token
     * @return
     */
    protected HttpEntity getHttpEntity(Object data, String token) {
        MultiValueMap<String, String> header = new HttpHeaders();
        header.add("Authorization", token);
        HttpEntity httpEntity = new HttpEntity(data, header);
        return httpEntity;
    }
 
    /**
     * 直接传入header
     *
     * @param data
     * @param header
     * @return
     */
    protected HttpEntity getHttpEntity(Object data, MultiValueMap header) {
        HttpEntity httpEntity = new HttpEntity(data, header);
        return httpEntity;
    }
 
}