Kyrexar

[SSR] webB

Nov 22nd, 2019 (edited)
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. // Petición HTTP​
  2. URL url = new URL("https://www.google.es");
  3. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  4. con.setRequestMethod("GET");
  5.  
  6. // Leer la respuesta​
  7. int status = con.getResponseCode();
  8. BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
  9. String inputLine;
  10. StringBuffer content = new StringBuffer();
  11. while ((inputLine = in.readLine()) != null) {
  12.     content.append(inputLine);
  13. }
  14.  
  15. in.close();
  16.  
  17. con.disconnect(); // Cerrar conexión​
  18.  
  19. // Construir respuesta
  20. public class FullResponseBuilder {
  21.     public static String getFullResponse(HttpURLConnection con) throws IOException {
  22.         StringBuilder fullResponseBuilder = new StringBuilder();
  23.  
  24.         // read status and message
  25.  
  26.         // read headers
  27.  
  28.         // read response content
  29.  
  30.         return fullResponseBuilder.toString();
  31.     }
  32. }
  33.  
  34. fullResponseBuilder.append(con.getResponseCode())
  35.   .append(" ")
  36.   .append(con.getResponseMessage())
  37.   .append("\n");
  38.  
  39. con.getHeaderFields().entrySet().stream()
  40.   .filter(entry -> entry.getKey() != null)
  41.   .forEach(entry -> {
  42.       fullResponseBuilder.append(entry.getKey()).append(": ");
  43.       List headerValues = entry.getValue();
  44.       Iterator it = headerValues.iterator();
  45.       if (it.hasNext()) {
  46.           fullResponseBuilder.append(it.next());
  47.           while (it.hasNext()) {
  48.               fullResponseBuilder.append(", ").append(it.next());
  49.           }
  50.       }
  51.       fullResponseBuilder.append("\n");
  52. });
Add Comment
Please, Sign In to add comment