1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.io.BufferedReader import java.io.InputStreamReader import java.net.HttpURLConnection import java.net.URL fun main() { for (i in 1..100) { val url = "https://www.com/$i/" try { val connection = URL(url).openConnection() as HttpURLConnection connection.requestMethod = "GET" val responseCode = connection.responseCode if (responseCode == HttpURLConnection.HTTP_OK) { val reader = BufferedReader(InputStreamReader(connection.inputStream)) var line: String? val response = StringBuilder() while (reader.readLine().also { line = it } != null) { response.append(line) } reader.close() println("Respuesta de $url: ${response.toString()}") } else { println("La solicitud GET a $url falló con el código de respuesta: $responseCode") } connection.disconnect() } catch (e: Exception) { println("Error al realizar la solicitud GET a $url: ${e.message}") } } } |