Handling a response body as an input stream

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));
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset