title: Read HTTP response header and body from one HTTP request in java
body: <p>In my program I need to send a HTTP request and read the HTTP response body and headers.</p>
  
  <p>So I added those examples together as follows;</p>
  
  <pre><code>URL obj = new URL("http://localhost:8080/SpringSecurity/admin");
  URLConnection conn = obj.openConnection();
  
  //get all headers
  Map&lt;String, List&lt;String&gt;&gt; map = conn.getHeaderFields();
  for (Map.Entry&lt;String, List&lt;String&gt;&gt; entry : map.entrySet()) {
      System.out.println("Key : " + entry.getKey() + " ,Value : " + entry.getValue());
  }        
  ByteArrayOutputStream output = (ByteArrayOutputStream) conn.getOutputStream();        
  
  byte[] input = output.toByteArray();         
  System.out.println(input.length);
  </code></pre>
  
  <p>It prints the headers but it doesn't print the length of byte array <code>input</code>.</p>
  
  <p>Can someone explain why this is happening and example of reading both HTTP response headers and body. </p>