Java URL 与 HTTP 访问
URL 和 HTTP 访问是 Web 开发的基础。理解 URL 和 HTTP 客户端的使用是进行 Web 应用开发的关键。本章将详细介绍 Java 中的 URL 和 HTTP 访问。
URL / URI 类
URL 类
URL(Uniform Resource Locator)表示统一资源定位符。
import java.net.URL;
// 创建 URL
URL url = new URL("https://www.example.com:8080/path?param=value#fragment");
// 获取 URL 组成部分
String protocol = url.getProtocol(); // "https"
String host = url.getHost(); // "www.example.com"
int port = url.getPort(); // 8080
String path = url.getPath(); // "/path"
String query = url.getQuery(); // "param=value"
String ref = url.getRef(); // "fragment"
URI 类
URI(Uniform Resource Identifier)表示统一资源标识符,比 URL 更通用。
import java.net.URI;
// 创建 URI
URI uri = new URI("https://www.example.com/path?param=value");
// URI 操作
URI base = new URI("https://www.example.com");
URI resolved = base.resolve("/path"); // 解析相对路径
URI relativized = base.relativize(resolved); // 获取相对路径
URL vs URI
| 特性 | URL | URI |
|---|---|---|
| 范围 | 资源定位符 | 资源标识符 |
| 关系 | URL 是 URI 的子集 | URI 更通用 |
| 使用 | 网络资源访问 | 资源标识 |
HttpURLConnection 使用
基本使用
HttpURLConnection 用于发送 HTTP 请求。
import java.net.*;
import java.io.*;
// 创建连接
URL url = new URL("https://www.example.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法
connection.setRequestMethod("GET");
// 设置请求头
connection.setRequestProperty("User-Agent", "Java Client");
connection.setRequestProperty("Accept", "application/json");
// 获取响应码
int responseCode = connection.getResponseCode();
// 读取响应
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
connection.disconnect();
设置连接属性
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 请求方法
connection.setRequestMethod("GET");
connection.setRequestMethod("POST");
// 超时设置
connection.setConnectTimeout(5000); // 连接超时
connection.setReadTimeout(10000); // 读取超时
// 请求头
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "Bearer token");
// 允许输入输出
connection.setDoInput(true);
connection.setDoOutput(true);
// 不使用缓存
connection.setUseCaches(false);
GET 与 POST 请求
GET 请求
import java.net.*;
import java.io.*;
public class HttpGetExample {
public static String sendGet(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "Java Client");
connection.setConnectTimeout(5000);
connection.setReadTimeout(10000);
int responseCode = connection.getResponseCode();
System.out.println("响应码:" + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
return response.toString();
}
} else {
return "请求失败,响应码:" + responseCode;
}
}
public static void main(String[] args) throws IOException {
String response = sendGet("https://www.example.com");
System.out.println(response);
}
}
POST 请求
import java.net.*;
import java.io.*;
public class HttpPostExample {
public static String sendPost(String urlString, String data) throws IOException {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
connection.setConnectTimeout(5000);
connection.setReadTimeout(10000);
// 发送数据
try (OutputStream os = connection.getOutputStream()) {
byte[] input = data.getBytes("UTF-8");
os.write(input);
os.flush();
}
int responseCode = connection.getResponseCode();
System.out.println("响应码:" + responseCode);
// 读取响应
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
return response.toString();
}
} else {
// 读取错误响应
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getErrorStream()))) {
StringBuilder error = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
error.append(line);
}
return "错误:" + error.toString();
}
}
}
public static void main(String[] args) throws IOException {
String jsonData = "{\"name\":\"张三\",\"age\":20}";
String response = sendPost("https://api.example.com/users", jsonData);
System.out.println(response);
}
}
示例:下载网页内容
示例 1:下载网页
import java.net.*;
import java.io.*;
public class WebPageDownloader {
public static String downloadPage(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
connection.setConnectTimeout(5000);
connection.setReadTimeout(10000);
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
return content.toString();
}
} else {
throw new IOException("HTTP 错误:" + responseCode);
}
}
public static void main(String[] args) {
try {
String content = downloadPage("https://www.example.com");
System.out.println(content);
} catch (IOException e) {
e.printStackTrace();
}
}
}