Handling a body response as InputStream can be accomplished using BodyHandlers.ofInputStream(), as shown in the following snippet of code:
HttpResponse<InputStream> responseOfInputStream = client.send(
request, HttpResponse.BodyHandlers.ofInputStream());
System.out.println(" HttpResponse.BodyHandlers.ofInputStream():");
System.out.println("Status code: "
+ responseOfInputStream.statusCode());
byte[] allBytes;
try (InputStream fromIs = responseOfInputStream.body()) {
allBytes = fromIs.readAllBytes();
}
System.out.println("Body: "
+ new String(allBytes, StandardCharsets.UTF_8));