Commit 92875883 authored by Miguel Reboiro-Jato's avatar Miguel Reboiro-Jato

Reorders ESEIHomeDownload examples and fixes URL creation

parent f2fe0ec0
/*-
* #%L
* Ejemplos de DAI - Sockets
* %%
* Copyright (C) 2014 - 2026 Miguel Reboiro Jato
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package es.uvigo.esei.dai.sockets.example5; 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.URI; import java.net.URI;
import java.net.http.HttpClient; import java.net.URL;
import java.net.http.HttpRequest; import java.net.URLConnection;
import java.net.http.HttpResponse;
public class ESEIHomeDownload { public class ESEIHomeDownload {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
final HttpClient client = HttpClient.newHttpClient(); final URL url = URI.create("https://www.esei.uvigo.es").toURL();
final HttpRequest request = HttpRequest.newBuilder() final URLConnection connection = url.openConnection();
.uri(URI.create("https://esei.uvigo.es")) System.out.println(connection.getClass());
.GET()
.build();
final HttpResponse<String> response = client try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
.send(request, HttpResponse.BodyHandlers.ofString()); String line;
while ((line = br.readLine()) != null) {
System.out.println(response.body()); System.out.println(line);
} catch (final Exception e) { }
}
} catch (final MalformedURLException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
/*-
* #%L
* Ejemplos de DAI - Sockets
* %%
* Copyright (C) 2014 - 2026 Miguel Reboiro Jato
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package es.uvigo.esei.dai.sockets.example6; package es.uvigo.esei.dai.sockets.example6;
import java.io.BufferedReader; import java.net.URI;
import java.io.IOException; import java.net.http.HttpClient;
import java.io.InputStreamReader; import java.net.http.HttpRequest;
import java.net.MalformedURLException; import java.net.http.HttpResponse;
import java.net.URL;
import java.net.URLConnection;
public class ESEIHomeDownload { public class ESEIHomeDownload {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
final URL url = new URL("https://www.esei.uvigo.es"); final HttpClient client = HttpClient.newHttpClient();
final URLConnection connection = url.openConnection(); final HttpRequest request = HttpRequest.newBuilder()
System.out.println(connection.getClass()); .uri(URI.create("https://esei.uvigo.es"))
.GET()
.build();
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { final HttpResponse<String> response = client
String line; .send(request, HttpResponse.BodyHandlers.ofString());
while ((line = br.readLine()) != null) {
System.out.println(line); System.out.println(response.body());
} } catch (final Exception e) {
}
} catch (final MalformedURLException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment