java.net.URL 封裝統一資源定位符,支援協議、主機、端口、路徑、查詢、片段等組件解析與構造。
import java.net.*;
public class GetHost {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java GetHost ...");
return;
}
for (String arg : args) {
try {
InetAddress[] addrs = InetAddress.getAllByName(arg);
for (InetAddress addr : addrs) {
System.out.println(addr.getHostName() + " -> " + addr.getHostAddress());
}
} catch (UnknownHostException e) {
System.err.println("無法解析:" + arg);
}
}
}
}
編譯運行:javac GetHost.java && java GetHost 114.114.114.114 www.baidu.com
import java.io.*;
import java.net.*;
public class GetHTML {
public static void main(String[] args) {
if (args.length == 0) {
System.err.println("Usage: java GetHTTP ");
System.exit(1);
}
try {
URL url = new URL(args[0]);
try (BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream(), "UTF-8"))) {
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
}
} catch (MalformedURLException e) {
System.err.println("URL 格式錯誤:" + e.getMessage());
} catch (IOException e) {
System.err.println("網絡讀取失敗:" + e.getMessage());
}
}
}
| 方法 | 說明 | 示例輸出 |
|---|---|---|
| getProtocol() | 協議 | http |
| getHost() | 主機名 | www.example.com |
| getPort() | 端口(-1 表示默認) | 8080 |
| getFile() | 路徑+查詢串 | /path?k=v |
| getRef() | 片段標識符 | section1 |
構造方式:
new URL("https://host:8080/path") 絕對 URLnew URL(baseURL, "relative/path") 相對 URLnew URL("http", "host", 8080, "/path") 組件拼裝URL url = new URL("https://api.example.com/data");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
conn.setConnectTimeout(5000);
conn.setReadTimeout(10000);
try (OutputStream os = conn.getOutputStream()) {
os.write("{\"key\":\"value\"}".getBytes("UTF-8"));
}
int code = conn.getResponseCode();
try (BufferedReader br = new BufferedReader(
new InputStreamReader(code < 400 ? conn.getInputStream() : conn.getErrorStream()))) {
// 處理響應
}
TrustManager),生產必須驗證。RandomAccessFile + Range 請求頭實現斷點續傳。