From a8722a8a91cc0622e6e825f9e66fcaa44839ae88 Mon Sep 17 00:00:00 2001 From: SimonGuzman Date: Mon, 9 Sep 2024 13:30:17 -0500 Subject: [PATCH] Ejercicio inicial completado, revisar si se puede optimizar mas o hacer de otra manera --- .../java/simonguzman.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Roadmap/20 - PETICIONES HTTP/java/simonguzman.java diff --git a/Roadmap/20 - PETICIONES HTTP/java/simonguzman.java b/Roadmap/20 - PETICIONES HTTP/java/simonguzman.java new file mode 100644 index 0000000000..b40679ca61 --- /dev/null +++ b/Roadmap/20 - PETICIONES HTTP/java/simonguzman.java @@ -0,0 +1,35 @@ + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +public class simonguzman { + public static void main(String[] args) throws IOException{ + makeHttpRequest("https://www.google.com/"); + } + + public static void makeHttpRequest(String url) throws IOException{ + URL obj = new URL(url); + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); + + con.setRequestMethod("GET"); + + int responseCode = con.getResponseCode(); + if (responseCode == 200) { + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuffer response = new StringBuffer(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + + System.out.println(response.toString()); + }else{ + System.out.println("ERROR: "+responseCode); + } + } +}