In my program I need to send a HTTP request and read the HTTP response body and headers.
So I added those examples together as follows;
URL obj = new URL("http://localhost:8080/SpringSecurity/admin");
URLConnection conn = obj.openConnection();
//get all headers
Map<String, List<String>> map = conn.getHeaderFields();
for (Map.Entry<String, List<String>> 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);
It prints the headers but it doesn't print the length of byte array input
.
Can someone explain why this is happening and example of reading both HTTP response headers and body.