From f2fe0ec0e40911d84a2bdf0d48d26b4334503d54 Mon Sep 17 00:00:00 2001 From: Miguel Reboiro-Jato Date: Mon, 21 Sep 2026 10:39:02 +0200 Subject: [PATCH] Adds HttpClient example --- .../sockets/example5/ESEIHomeDownload.java | 32 ++++++++----------- .../sockets/example6/ESEIHomeDownload.java | 31 ++++++++++++++++++ 2 files changed, 45 insertions(+), 18 deletions(-) create mode 100644 src/main/java/es/uvigo/esei/dai/sockets/example6/ESEIHomeDownload.java diff --git a/src/main/java/es/uvigo/esei/dai/sockets/example5/ESEIHomeDownload.java b/src/main/java/es/uvigo/esei/dai/sockets/example5/ESEIHomeDownload.java index 49b6bd4..c499a60 100644 --- a/src/main/java/es/uvigo/esei/dai/sockets/example5/ESEIHomeDownload.java +++ b/src/main/java/es/uvigo/esei/dai/sockets/example5/ESEIHomeDownload.java @@ -21,30 +21,26 @@ */ package es.uvigo.esei.dai.sockets.example5; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; public class ESEIHomeDownload { public static void main(String[] args) { try { - final URL url = new URL("https://www.esei.uvigo.es"); + final HttpClient client = HttpClient.newHttpClient(); - final URLConnection connection = url.openConnection(); - System.out.println(connection.getClass()); + final HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("https://esei.uvigo.es")) + .GET() + .build(); - try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { - String line; - while ((line = br.readLine()) != null) { - System.out.println(line); - } - } - } catch (final MalformedURLException e) { - e.printStackTrace(); - } catch (final IOException e) { + final HttpResponse response = client + .send(request, HttpResponse.BodyHandlers.ofString()); + + System.out.println(response.body()); + } catch (final Exception e) { e.printStackTrace(); } } diff --git a/src/main/java/es/uvigo/esei/dai/sockets/example6/ESEIHomeDownload.java b/src/main/java/es/uvigo/esei/dai/sockets/example6/ESEIHomeDownload.java new file mode 100644 index 0000000..df0afe3 --- /dev/null +++ b/src/main/java/es/uvigo/esei/dai/sockets/example6/ESEIHomeDownload.java @@ -0,0 +1,31 @@ +package es.uvigo.esei.dai.sockets.example6; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +public class ESEIHomeDownload { + public static void main(String[] args) { + try { + final URL url = new URL("https://www.esei.uvigo.es"); + + final URLConnection connection = url.openConnection(); + System.out.println(connection.getClass()); + + try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { + String line; + while ((line = br.readLine()) != null) { + System.out.println(line); + } + } + } catch (final MalformedURLException e) { + e.printStackTrace(); + } catch (final IOException e) { + e.printStackTrace(); + } + } +} + -- 2.18.1