Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created February 11, 2019 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/d8628a4c49e7f6a119404d7898dd0bb9 to your computer and use it in GitHub Desktop.
Save parzibyte/d8628a4c49e7f6a119404d7898dd0bb9 to your computer and use it in GitHub Desktop.
public static String peticionHttpGet(String urlParaVisitar) throws Exception {
// Esto es lo que vamos a devolver
StringBuilder resultado = new StringBuilder();
// Crear un objeto de tipo URL
URL url = new URL(urlParaVisitar);
// Abrir la conexión e indicar que será de tipo GET
HttpURLConnection conexion = (HttpURLConnection) url.openConnection();
conexion.setRequestMethod("GET");
// Búferes para leer
BufferedReader rd = new BufferedReader(new InputStreamReader(conexion.getInputStream()));
String linea;
// Mientras el BufferedReader se pueda leer, agregar contenido a resultado
while ((linea = rd.readLine()) != null) {
resultado.append(linea);
}
// Cerrar el BufferedReader
rd.close();
// Regresar resultado, pero como cadena, no como StringBuilder
return resultado.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment