IOUtils { public static Result<String, IOException> fetchContent(String url) { InputStream stream = null; try { URLConnection connection = createConnection(url); stream = connection.getInputStream(); String encoding = connection.getContentEncoding(); Charset charset = encoding == null ? StandardCharsets.UTF_8 : Charset.forName(encoding); return IOUtils.convertStreamToString(stream, charset); } catch (IOException exception) { return Result.error(exception); } finally { close(stream); } } private  IOUtils(); static Result<String, IOException> fetchContent(String url); static Result<InputStream, IOException> fetchContentAsStream(String url); static InputStream convertStringToStream(String str); static Result<String, IOException> convertStreamToString(InputStream inputStream); static Result<String, IOException> convertStreamToString(InputStream inputStream, Charset encoding); static void close(@Nullable Closeable closeable); static void close(@Nullable Closeable closeable, @Nullable Consumer<IOException> exceptionConsumer); static final int DEFAULT_BUFFER_SIZE; }
@Test void getURLContent() { Result<String, IOException> result = IOUtils.fetchContent("https: assertTrue(result.isOk()); assertNotNull(result.get()); assertTrue(result.get().contains("<html")); }