Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Petición HTTP
- URL url = new URL("https://www.google.es");
- HttpURLConnection con = (HttpURLConnection) url.openConnection();
- con.setRequestMethod("GET");
- // Leer la respuesta
- int status = con.getResponseCode();
- BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
- String inputLine;
- StringBuffer content = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- content.append(inputLine);
- }
- in.close();
- con.disconnect(); // Cerrar conexión
- // Construir respuesta
- public class FullResponseBuilder {
- public static String getFullResponse(HttpURLConnection con) throws IOException {
- StringBuilder fullResponseBuilder = new StringBuilder();
- // read status and message
- // read headers
- // read response content
- return fullResponseBuilder.toString();
- }
- }
- fullResponseBuilder.append(con.getResponseCode())
- .append(" ")
- .append(con.getResponseMessage())
- .append("\n");
- con.getHeaderFields().entrySet().stream()
- .filter(entry -> entry.getKey() != null)
- .forEach(entry -> {
- fullResponseBuilder.append(entry.getKey()).append(": ");
- List headerValues = entry.getValue();
- Iterator it = headerValues.iterator();
- if (it.hasNext()) {
- fullResponseBuilder.append(it.next());
- while (it.hasNext()) {
- fullResponseBuilder.append(", ").append(it.next());
- }
- }
- fullResponseBuilder.append("\n");
- });
Add Comment
Please, Sign In to add comment