| 方式 | 適用場景 | 特點 |
|---|---|---|
URL.openStream() | 簡單 GET、無需 Header/代理 | 一行代碼,無超時控制 |
HttpURLConnection | 生產環境:需設置超時、Header、代理、Cookie、POST | 功能完整、標準庫零依賴 |
public static String getContent(String urlStr) {
try (BufferedReader br = new BufferedReader(
new InputStreamReader(new URL(urlStr).openStream(), "UTF-8"))) {
return br.lines().collect(Collectors.joining("\n"));
} catch (Exception e) {
return "error: " + e.getMessage();
}
}
public class HttpClientUtil {
public static String get(String urlStr, String proxyHost, int proxyPort,
String username, String password) throws IOException {
// 1. 代理設置
if (proxyHost != null) {
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
});
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", String.valueOf(proxyPort));
}
// 2. 打開連接
HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection();
conn.setConnectTimeout(10_000);
conn.setReadTimeout(30_000);
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; JavaBot)");
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml");
conn.connect();
// 3. 讀取響應
try (InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
return br.lines().collect(Collectors.joining("\n"));
}
}
}
Content-Type 中的 charset,回退 UTF-8,再用 cpDetector 或 juniversalchardetconn.setInstanceFollowRedirects(true)(默認開啟)conn.setRequestProperty("Accept-Encoding", "gzip, deflate"),讀取時用 GZIPInputStream 解壓HttpURLConnection 無連接池SocketTimeoutException、ConnectException、SSLHandshakeException 做重試/告警標準庫 HttpURLConnection 足夠應對簡單爬蟲、內部服務調用;涉及連接池、連接保活、HTTP/2、異步非阻塞時,請直接引入 OkHttp 或 Apache HttpClient 5,避免造輪子。