Завантаження веб-сторінки

Java:
Завантаження веб-сторінки

How to: (Як зробити:)

To download a web page in Java, use java.net.HttpURLConnection. Here’s a concise example:

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class WebPageDownloader {
    
    public static void main(String[] args) throws IOException {
        URL url = new URL("http://www.example.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } finally {
            connection.disconnect();
        }
    }
}

Sample output:

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
    ...
</html>

Deep Dive (Поглиблений аналіз):

Downloading web pages has come a long way from the days of raw socket connections. Now, HttpURLConnection is the basic Java class for it, but it’s not the only tool. Libraries like Apache HttpClient or Jsoup provide more functionality and are more user-friendly.

There’s a trade-off: HttpURLConnection is built-in and doesn’t need extra jars, while third-party libraries require managing dependencies but can simplify tasks like handling cookies, or managing sessions.

Historically, Java’s HttpURLConnection has been criticized for being clunky and less intuitive compared to what modern libraries offer. Yet it remains a go-to option for many, especially when sticking to the standard library is appealing or for simpler tasks where an external library might be overkill.

See Also (Дивіться також):

For more info, check out: