Java中发送Http请求之HttpURLConnectio

这是我参与11月更文挑战的第11天,活动详情查看:11月更文挑战

Java中发送Http请求的方式有很多,记录一下相关的请求方式,本次记录Jdk自带的HttpURLConnection,简单简洁,使用简单.

1 HttpURLConnection

HttpURLConnection是Jdk自带的请求工具,不用依赖第三方jar包,适用简单的场景使用.

使用方式是, 通过调用URL.openConnection方法,得到一个URLConnection对象,并强转为HttpURLConnection对象.

java.net.URL部分源码:

1
2
3
java复制代码    public URLConnection openConnection() throws java.io.IOException {
return handler.openConnection(this);
}

java.net.HttpURLConnection部分源码:

1
2
3
java复制代码abstract public class HttpURLConnection extends URLConnection {
// ...
}

从代码可知HttpURLConnection是URLConnection子类, 其内类中主要放置的是一些父类的方法和请求码信息.

发送GET请求, 其主要的参数从URI中获取,还有请求头,cookies等数据.

发送POST请求, HttpURLConnection实例必须设置setDoOutput(true),其请求体数据写入由HttpURLConnection的getOutputStream()方法返回的输出流来传输数据.

1 准备一个SpringBoot项目环境

2 添加一个控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
java复制代码@Controller
@Slf4j
public class HelloWorld {

@Override
@GetMapping("/world/getData")
@ResponseBody
public String getData(@RequestParam Map<String, Object> param) {
System.out.println(param.toString());
return "<h1> Hello World getData 方法</h1>";
}

@PostMapping("/world/getResult")
@ResponseBody
public String getResult(@RequestBody Map<String, Object> param) {
System.out.println(param.toString());
return "<h1> Hello World getResult 方法</h1>";
}
}

3 添加一个发送请求的工具类

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
java复制代码package com.cf.demo.http;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
* Http请求工具类
*/
@Slf4j
@Data
public class JdkHttpUtils {

/**
* 获取 POST请求
*
* @return 请求结果
*/
public static String getPost(String url, Integer connectTimeout,
Integer readTimeout, String contentType, Map<String, String> heads,
Map<String, String> params) throws IOException {

URL u;
HttpURLConnection connection = null;
OutputStream out;
try {
u = new URL(url);
connection = (HttpURLConnection) u.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(connectTimeout);
connection.setReadTimeout(readTimeout);
connection.setRequestProperty("Content-Type", contentType);
// POST请求必须设置该属性
connection.setDoOutput(true);
connection.setDoInput(true);
if (heads != null) {
for (Map.Entry<String, String> stringStringEntry : heads.entrySet()) {
connection.setRequestProperty(stringStringEntry.getKey(),
stringStringEntry.getValue());
}
}

out = connection.getOutputStream();
if (params != null && !params.isEmpty()) {
out.write(toJSONString(params).getBytes());
}
out.flush();
out.close();

// 获取请求返回的数据流
InputStream is = connection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 封装输入流is,并指定字符集
int i;
while ((i = is.read()) != -1) {
baos.write(i);
}
return baos.toString();

} catch (Exception e) {
log.error("请求发生异常,信息为= {} ", e.getMessage());
}
return null;
}


/**
* 获取 POST请求
*
* @return 请求结果
*/
public static String getGet(String url, Integer connectTimeout,
Integer readTimeout, String contentType, Map<String, String> heads,
Map<String, String> params) throws IOException {

// 拼接请求参数
if (params != null && !params.isEmpty()) {
url += "?";
if (params != null && !params.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> stringObjectEntry : params.entrySet()) {
try {
sb.append(stringObjectEntry.getKey()).append("=").append(
URLEncoder.encode(stringObjectEntry.getValue(), "UTF-8"))
.append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
sb.delete(sb.length() - 1, sb.length());
url += sb.toString();
}
}

URL u;
HttpURLConnection connection;
u = new URL(url);
connection = (HttpURLConnection) u.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(connectTimeout);
connection.setReadTimeout(readTimeout);
connection.setRequestProperty("Content-Type", contentType);
if (heads != null) {
for (Map.Entry<String, String> stringStringEntry : heads.entrySet()) {
connection.setRequestProperty(stringStringEntry.getKey(),
stringStringEntry.getValue());
}
}

// 获取请求返回的数据流
InputStream is = connection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 封装输入流is,并指定字符集
int i;
while ((i = is.read()) != -1) {
baos.write(i);
}
return baos.toString();

}


/**
* Map转Json字符串
*/
public static String toJSONString(Map<String, String> map) {
Iterator<Entry<String, String>> i = map.entrySet().iterator();
if (!i.hasNext()) {
return "{}";
}

StringBuilder sb = new StringBuilder();
sb.append('{');
for (; ; ) {
Map.Entry<String, String> e = i.next();
String key = e.getKey();
String value = e.getValue();
sb.append("\"");
sb.append(key);
sb.append("\"");
sb.append(':');
sb.append("\"");
sb.append(value);
sb.append("\"");
if (!i.hasNext()) {
return sb.append('}').toString();
}
sb.append(',').append(' ');
}
}


}

4 添加测试工具类

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
java复制代码@Slf4j
public class HttpTest {

// 请求地址
public String url = "";
// 请求头参数
public Map<String, String> heads = new HashMap<>();
// 请求参数
public Map<String, String> params = new HashMap<>();
// 数据类型
public String contentType = "application/json";
// 连接超时
public Integer connectTimeout = 15000;
// 读取超时
public Integer readTimeout = 60000;

@Test
public void testGET() {
// 1 添加数据
url = "http://localhost:8080/world/getData";
heads.put("token", "hhhhhhhhhhhaaaaaaaa");
params.put("username", "libai");

String result = null;
try {
// 2 发送Http请求,获取返回结果
result = JdkHttpUtils
.getGet(url, connectTimeout, readTimeout, contentType, heads, params);
} catch (IOException e) {
e.printStackTrace();
}
// 3 打印结果
log.info(result);
}

@Test
public void testPOST() {
// 1 添加数据
url = "http://localhost:8080/world/getResult";
heads.put("token", "hhhhhhhhhhhaaaaaaaa");
params.put("username", "libai");

String result = null;
try {
// 2 发送Http请求,获取返回结果
result = JdkHttpUtils
.getPost(url, connectTimeout, readTimeout, contentType, heads, params);
} catch (IOException e) {
e.printStackTrace();
}
// 3 打印结果
log.info(result);
}
}

5 测试结果

POST请求测试结果

1
2
3
4
5
java复制代码/*
[main] INFO com.cf.demo.HttpTest - <h1> Hello World getResult 方法</h1>

{username=libai}
*/

GET请求测试结果

1
2
3
4
5
6
java复制代码/*
[main] INFO com.cf.demo.HttpTest - <h1> Hello World getData方法</h1>

{username=libai}

*/

参考资料:

www.baeldung.com/java-http-r…

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%