zxl
8 小时以前 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
package com.ycl.utils.http;
 
import com.alibaba.fastjson2.JSON;
import com.ycl.properties.RequestProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
 
/**
 * 请求可重试
 *
 * @author:xp
 * @date:2024/4/18 14:30
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RetryHttpUtil {
 
    private final RequestProperties requestProperties;
 
    /**
     * get请求
     *
     * @param url 请求地址(带参数)
     * @param token token
     * @param returnType 响应数据类型
     * @param <T>
     * @return
     */
    public <T> T get(String url, String token, Class<T> returnType) {
        HttpClient httpClient = HttpClientBuilder
                .create()
                .setRetryHandler(new DefaultHttpRequestRetryHandler(requestProperties.getRetry(), true))
                .build();
        try {
            HttpGet request = new HttpGet(url);
            request.setHeader("Authorization", "Bearer " + token);
            HttpResponse response = httpClient.execute(request);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode >= 200 && statusCode < 300) {
                // 请求成功
                HttpEntity entity = response.getEntity();
                // json字符串转响应数据
                String responseBody = EntityUtils.toString(entity);
                T t = JSON.parseObject(responseBody, returnType);
                EntityUtils.consume(entity);
                return t;
            } else {
                // 请求失败
                log.error("Request failed with status code: " + statusCode);
            }
        } catch (IOException e) {
            // 请求发生异常
            log.error("Request failed with exception: " + e.getMessage());
        }
        return null;
    }
 
    /**
     * post请求
     *
     * @param url 请求地址
     * @param data 请求体参数
     * @param token token
     * @param returnType 返回的数据类型
     * @param <T>
     * @return
     */
    public <T> T post(String url, Object data, String token, Class<T> returnType) {
        HttpClient httpClient = HttpClientBuilder
                .create()
                .setRetryHandler(new DefaultHttpRequestRetryHandler(requestProperties.getRetry(), true))
                .build();
        try {
            HttpPost request = new HttpPost(url);
            request.setHeader("Authorization", "Bearer " + token);
            request.setHeader("Content-Type", "application/json;charset=UTF-8");
            StringEntity stringEntity = new StringEntity(JSON.toJSONString(data));
            request.setEntity(stringEntity);
            HttpResponse response = httpClient.execute(request);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode >= 200 && statusCode < 300) {
                // 请求成功
                HttpEntity entity = response.getEntity();
                // json字符串转响应数据
                String responseBody = EntityUtils.toString(entity);
                T t = JSON.parseObject(responseBody, returnType);
                EntityUtils.consume(entity);
                return t;
            } else {
                // 请求失败
                log.error("Request failed with status code: " + statusCode);
            }
        } catch (IOException e) {
            // 请求发生异常
            log.error("Request failed with exception: " + e.getMessage());
        }
        return null;
    }
 
}