04http

http

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
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
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.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class HttpUtil {

/**
* 日志
*/
private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);

/**
* 编码
*/
private static final String CHARSET = "UTF-8";

public static String get(String url) {
return execute(url);
}

public static String get(String url, Map<String, Object> params) {
url = url + "?";
for (Iterator<String> iterator = params.keySet().iterator(); iterator.hasNext();) {
String key = iterator.next();
String temp = key + "=" + params.get(key) + "&";
url = url + temp;
}
url = url.substring(0, url.length() - 1);
return execute(url);
}

/**
* http get请求
*/
private static String execute(String url) {
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
String str = EntityUtils.toString(entity, CHARSET);
return str;
}
} catch (Exception e) {
log.error("请求 " + url + "失败:" + e);
}
return null;
}

/**
* http post请求
* @return
*/
public static String post(String url, Map<String, Object> params) {
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
for (Iterator<String> iterator = params.keySet().iterator(); iterator.hasNext();) {
String key = iterator.next();
parameters.add(new BasicNameValuePair(key, params.get(key).toString()));
}
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(parameters, CHARSET);
httpPost.setEntity(uefEntity);
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
String str = EntityUtils.toString(entity, CHARSET);
return str;
}
} finally {
response.close();
httpClient.close();
}
} catch (Exception e) {
log.error("请求" + url + "失败:" + e);
throw new RuntimeException(e);
}
return null;
}

/**
* http post请求 json
* @return
*/
public static String post(String url, String params) {
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
StringEntity sEntity = new StringEntity(params, CHARSET);
httpPost.setEntity(sEntity);
httpPost.setHeader("Content-Type","application/json;charset=utf-8");
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity, CHARSET);
}
} finally {
response.close();
httpClient.close();
}
} catch (Exception e) {
log.error("请求" + url + "失败:" + e);
throw new RuntimeException(e);
}
return null;
}
}

04http
https://jiajun.xyz/2021/07/25/java/quickstart/04http/
作者
Lambda
发布于
2021年7月25日
许可协议