Java发起HTTP请求的工具类

ps:CloseableHttpClient 是 HttpClient 接口的实现类

1.发起GET请求

①无参数的GET请求

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 功能描述: 无参数的GET请求
*
* @param: [url]
* @return: java.lang.String
* @auther: wjy
* @date: 2019/5/31 22:06
*/
public static String doGet(String url) {

return doGet(url, null);
}

②有参数的GET请求

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
/**
* 功能描述: 有参数的GET请求
*
* @param: [url, param]
* @return: java.lang.String
* @auther: wjy
* @date: 2019/5/31 22:05
*/
public static String doGet(String url, LinkedHashMap<String, String> param) {

// 创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
// GET请求的返回内容
String resultString = "";

try {
URIBuilder builder = new URIBuilder(url);
// 设置请求参数
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
// 构建一个URI对象
URI uri = builder.build();
// 创建GET请求
HttpGet httpGet = new HttpGet(uri);
// 执行GET请求
response = httpClient.execute(httpGet);
// 获取响应状态码
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (IOException e) {
log.error("GET Method Catch Unexception Hanlder:{}, message:{}", e, e.getMessage());
throw new ServiceException(e.getMessage());
} finally {
try {
if (response != null) {
response.close();
}
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return resultString;
}

2.发起POST请求

①参数为Map对象的POST请求

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
/**
* 功能描述: 有参数(Map对象)的POST请求
*
* @param: [url, param]
* @return: java.lang.String
* @auther: wjy
* @date: 2019/5/31 22:06
*/
public static String doPost(String url, Map<String, String> param) {

// 创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
// POST请求的返回内容
String resultString = "";

try {
// 创建POST请求
HttpPost httpPost = new HttpPost(url);
// 创建请求参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 构造form表单式的实体
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
// 执行POST请求
response = httpClient.execute(httpPost);
// 获取响应状态码
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (IOException e) {
log.error("POST Method Catch Unexception Hanlder:{}, message:{}", e, e.getMessage());
throw new ServiceException(e.getMessage());
} finally {
try {
if (response != null) {
response.close();
}
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return resultString;
}

②参数为JSON字符串的POST请求

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
/**
* 功能描述: 有参数(Json字符串)的POST请求
*
* @param: [url, jsonData]
* @return: java.lang.String
* @author: wjy
* @date: 2019/6/25 17:39
*/
public static String doPost(String url, String jsonData) {

// 创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
// POST请求的返回内容
String resultString = "";

try {
// 创建POST请求
HttpPost httpPost = new HttpPost(url);
// 解析Json数据并构造实体
StringEntity stringEntity = new StringEntity(jsonData, ContentType.APPLICATION_JSON);
httpPost.setEntity(stringEntity);
// 执行POST请求
response = httpClient.execute(httpPost);
// 获取响应状态码
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (IOException e) {
log.error("POST Method Catch Exception Handler:{}, message:{}", e, e.getMessage());
} finally {
try {
if (response != null) {
response.close();
}
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return resultString;
}

附录

0%