如何使用java.net.URLConnection发起和处理HTTP请求

如何使用java.net.URLConnection发起和处理HTTP请求

技术背景

在Java开发中,经常需要与网络上的资源进行交互,发起HTTP请求是常见的操作。java.net.URLConnection 是Java标准库中用于处理URL连接的抽象类,它提供了一种方便的方式来发起和处理HTTP请求。不过从Java 11开始,也有了 java.net.http.HttpClient 这一相对简洁的API用于处理HTTP请求。

实现步骤

准备工作

首先需要知道请求的URL和字符集,参数是可选的,取决于功能需求。

1
2
3
4
5
6
7
8
9
String url = "http://example.com";
String charset = "UTF-8"; // Or in Java 7 and later, use the constant: java.nio.charset.StandardCharsets.UTF_8.name()
String param1 = "value1";
String param2 = "value2";
// ...

String query = String.format("param1=%s&param2=%s",
URLEncoder.encode(param1, charset),
URLEncoder.encode(param2, charset));

发起HTTP GET请求

1
2
3
4
URLConnection connection = new URL(url + "?" + query).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();
// ...

如果不需要设置任何头信息,还可以使用 URL#openStream() 快捷方法:

1
2
InputStream response = new URL(url).openStream();
// ...

发起HTTP POST请求

1
2
3
4
5
6
7
8
9
10
11
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true); // Triggers POST.
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);

try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes(charset));
}

InputStream response = connection.getInputStream();
// ...

设置超时时间

1
2
3
HttpURLConnection httpConnection = (HttpURLConnection) new URL(url).openConnection();
httpConnection.setConnectTimeout(3000); // 3s
httpConnection.setReadTimeout(6000); // 6s

在使用基于Sun/Oracle的JRE时,对于POST请求,需要关闭读取超时重试机制:

1
System.setProperty("sun.net.http.retryPost", "false");

在Android开发中,需要使用以下变通方法:

1
httpConnection.setChunkedStreamingMode(0);

收集HTTP响应信息

获取HTTP响应状态

1
int status = httpConnection.getResponseCode();

获取HTTP响应头

1
2
3
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
System.out.println(header.getKey() + "=" + header.getValue());
}

获取HTTP响应编码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
String contentType = connection.getHeaderField("Content-Type");
String charset = null;

for (String param : contentType.replace(" ", "").split(";")) {
if (param.startsWith("charset=")) {
charset = param.split("=", 2)[1];
break;
}
}

if (charset != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response, charset))) {
for (String line; (line = reader.readLine()) != null;) {
// ... System.out.println(line)?
}
}
} else {
// It's likely binary content, use InputStream/OutputStream.
}

维护会话

1
2
3
4
5
6
// First set the default cookie manager.
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

// All the following subsequent URLConnections will use the same cookie manager.
URLConnection connection = new URL(url).openConnection();
// ...

设置流式传输模式

1
httpConnection.setFixedLengthStreamingMode(contentLength);

如果事先不知道内容长度,可以使用分块流式传输模式:

1
httpConnection.setChunkedStreamingMode(1024);

设置User-Agent

1
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");

错误处理

1
InputStream error = ((HttpURLConnection) connection).getErrorStream();

如果HTTP响应代码为 -1,可以关闭连接保持机制:

1
System.setProperty("http.keepAlive", "false");

上传文件

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
String param = "value";
File textFile = new File("/path/to/file.txt");
File binaryFile = new File("/path/to/file.bin");
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
String CRLF = "\r\n"; // Line separator required by multipart/form-data.
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

try (
OutputStream output = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
) {
// Send normal param.
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF).append(param).append(CRLF).flush();

// Send text file.
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + textFile.getName() + "\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset!
writer.append(CRLF).flush();
Files.copy(textFile.toPath(), output);
output.flush(); // Important before continuing with writer!
writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

// Send binary file.
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
Files.copy(binaryFile.toPath(), output);
output.flush(); // Important before continuing with writer!
writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

// End of multipart/form-data.
writer.append("--" + boundary + "--").append(CRLF).flush();
}

处理不受信任或配置错误的HTTPS站点

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
static {
TrustManager[] trustAllCertificates = new TrustManager[] {
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null; // Not relevant.
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
// Do nothing. Just allow them all.
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
// Do nothing. Just allow them all.
}
}
};

HostnameVerifier trustAllHostnames = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true; // Just allow them all.
}
};

try {
System.setProperty("jsse.enableSNIExtension", "false");
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCertificates, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(trustAllHostnames);
}
catch (GeneralSecurityException e) {
throw new ExceptionInInitializerError(e);
}
}

核心代码

以下是一个完整的示例代码,展示了如何使用 java.net.URLConnection 发起HTTP GET和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
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class HttpExample {
public static void main(String[] args) throws IOException {
String url = "http://example.com";
String charset = StandardCharsets.UTF_8.name();
String param1 = "value1";
String param2 = "value2";
String query = String.format("param1=%s&param2=%s",
URLEncoder.encode(param1, charset),
URLEncoder.encode(param2, charset));

// HTTP GET request
URLConnection getConnection = new URL(url + "?" + query).openConnection();
getConnection.setRequestProperty("Accept-Charset", charset);
InputStream getResponse = getConnection.getInputStream();
try (Scanner scanner = new Scanner(getResponse)) {
String responseBody = scanner.useDelimiter("\\A").next();
System.out.println("GET Response: " + responseBody);
}

// HTTP POST request
URLConnection postConnection = new URL(url).openConnection();
postConnection.setDoOutput(true);
postConnection.setRequestProperty("Accept-Charset", charset);
postConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
try (OutputStream output = postConnection.getOutputStream()) {
output.write(query.getBytes(charset));
}
InputStream postResponse = postConnection.getInputStream();
try (Scanner scanner = new Scanner(postResponse)) {
String responseBody = scanner.useDelimiter("\\A").next();
System.out.println("POST Response: " + responseBody);
}
}
}

最佳实践

  • 对于Java 11及更高版本,优先考虑使用 java.net.http.HttpClient 进行HTTP请求,它提供了更简洁的API和更好的异步支持。
  • 在处理HTTP请求时,始终处理可能的异常,如 IOException 等。
  • 对于需要维护会话的场景,使用 CookieHandler 来管理cookie。
  • 为了避免内存溢出问题,对于大文件上传,使用流式传输模式。

常见问题

403 Forbidden错误

可能是服务器基于 User-Agent 请求头阻止了请求,可以通过设置 User-Agent 来解决。

连接超时或读取超时

可以通过设置 setConnectTimeout()setReadTimeout() 来解决。

不受信任的服务器证书

可以使用上述的 TrustManagerHostnameVerifier 来处理不受信任的服务器证书。


如何使用java.net.URLConnection发起和处理HTTP请求
https://119291.xyz/posts/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests/
作者
ww
发布于
2025年5月21日
许可协议