HttpClientUtil.java
3.89 KB
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
package com.ruoyi.utils;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;
/**
* @author liujun
* @date 2024年08月06日 16:54
*/
@Slf4j
@Component
public class HttpClientUtil {
public String doPost(String url, Map<String, Object> paraMap, Map<String, Object> head) {
HttpClient client = null;
HttpPost post = null;
try {
client = HttpClients.createDefault();
post = new HttpPost();
post.setURI(new URI(url));
if (MapUtils.isNotEmpty(paraMap)) {
// ArrayList<BasicNameValuePair> list = new ArrayList<>();
// Set<Map.Entry<String, Object>> entrySet = paraMap.entrySet();
// for (Map.Entry<String,Object> entry : entrySet) {
// String key = entry.getKey();
// String value = Objects.isNull(entry.getValue())?"":entry.getValue().toString();
// list.add(new BasicNameValuePair(key, value));
// }
// post.setEntity(new UrlEncodedFormEntity(list, org.apache.http.protocol.HTTP.UTF_8));
StringEntity entity = new StringEntity(JSON.toJSONString(paraMap), ContentType.APPLICATION_JSON);
post.setEntity(entity);
}
if (MapUtils.isNotEmpty(head)) {
for (Map.Entry<String, Object> entry : head.entrySet()) {
String value = Objects.isNull(entry.getValue()) ? "" : entry.getValue().toString();
post.addHeader(entry.getKey(), value);
}
}
HttpResponse response = client.execute(post);
if (Objects.isNull(response) || response.getStatusLine().getStatusCode() != 200) {
log.info("[{}]请求数据错误", url, response.getEntity());
return null;
}
return EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (URISyntaxException e) {
log.error("请求数据,URL错误,请检查请求URL,[{}]", url, e);
} catch (ClientProtocolException e) {
log.error("请求数据错误,[{}]", url, e);
} catch (IOException e) {
log.error("请求数据错误,[{}]", url, e);
}
return null;
}
public static String get(String url) {
String resultContent = null;
HttpGet httpGet = new HttpGet(url);
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
HttpEntity entity = response.getEntity();
// 获取响应信息
resultContent = EntityUtils.toString(entity);
if(StringUtils.isNotEmpty(resultContent)){
resultContent = new String(resultContent.getBytes(),StandardCharsets.UTF_8);
}
}
} catch (IOException | ParseException e) {
e.printStackTrace();
}
return resultContent;
}
}