Programming Tips - Java: Download a webpage with platform agnostic Java

Date: 2022dec6 Update: 2025sep21 Language: Java Platform: non-Andr_id Keywords: web, page, http, https Q. Java: Download a webpage with platform agnostic Java A. Here is a full example:
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URI; import java.net.URL; import java.security.cert.CertificateException; class Demo { // Helpers static int range(final int n) { return n / 100 * 100; } static String readStream(InputStream is) throws IOException { if (is == null) return ""; BufferedReader br = null; try { StringBuilder sb = new StringBuilder(); br = new BufferedReader(new InputStreamReader(is)); String line; while ((line = br.readLine()) != null) { sb.append(line); sb.append("\n"); } return sb.toString(); } finally { if (br != null) { br.close(); } } } static String readContent(HttpURLConnection uc) throws IOException { return readStream(uc.getInputStream()); } static String readError(HttpURLConnection uc) throws IOException { return readStream(uc.getErrorStream()); } // Plain Java (non-Andr_id) static String getUrlDirect(final String strUrl) throws URISyntaxException, IOException, MalformedURLException, CertificateException { HttpURLConnection uc = null; String strContent = null; try { final URI uri = new URI(strUrl); final URL url = uri.toURL(); uc = (HttpURLConnection)url.openConnection(); uc.connect(); final int iResponseCode = uc.getResponseCode(); final int iResponseRange = range(iResponseCode); final String strResponseMessage = uc.getResponseMessage(); final String strContentType = uc.getContentType(); if (iResponseRange == HttpURLConnection.HTTP_OK) { strContent = readContent(uc); } else { strContent = readError(uc); } } finally { if (uc != null) { uc.disconnect(); } } return strContent; } public static final void main(String []args) throws Exception { String content = getUrlDirect("https://example.com"); System.out.println("content=" + content); } }
Output:
content=<!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type="text/css"> body { ...