Reading a web page in Java
last modified September 18, 2026
Reading a web page in Java is a tutorial that presents several ways to read a web page in Java. It contains seven examples of downloading HTML source from a small web page.
Java tools for reading web pages
Java has built-in tools and third-party libraries for reading and downloading web pages. In the examples, we use HttpClient, URL, jsoup, HtmlCleaner, Apache HttpClient, Jetty HttpClient, and HtmlUnit.
In the following examples, we download HTML source from the example.com website.
Reading a web page with HttpClient
Java 11 introduced the HttpClient API.
package com.zetcode;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class ReadWebPage {
public static void main(String[] args) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://example.com"))
.GET() // GET is default
.build();
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}
We use the Java HttpClient to download the web page.
HttpClient client = HttpClient.newHttpClient();
A new HttpClient is created with the newHttpClient
factory method.
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://example.com"))
.build();
We build a synchronous request to the web page. The default method is GET.
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
We send the request and retrieve the content of the response and print it
to the console. We use HttpResponse.BodyHandlers.ofString
since we expect a string HTML response.
Reading a web page with URL
URL represents a Uniform Resource Locator, a pointer to a resource
on the World Wide Web.
package com.zetcode;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
public class ReadWebPageEx {
public static void main(String[] args) throws IOException {
var url = new URL("https://example.com");
try (var br = new BufferedReader(new InputStreamReader(url.openStream()))) {
String line;
var sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append(System.lineSeparator());
}
System.out.println(sb);
}
}
}
The code example reads the contents of a web page.
try (var br = new BufferedReader(new InputStreamReader(url.openStream()))) {
The openStream method opens a connection to the specified URL and returns an
InputStream for reading from that connection. The InputStreamReader is
a bridge from byte streams to character streams. It reads bytes and decodes them into characters
using a specified charset. In addition, BufferedReader is used for better performance.
var sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append(System.lineSeparator());
}
The HTML data is read line by line with the readLine method. The source
is appended to the StringBuilder.
System.out.println(sb);
In the end, the contents of the StringBuilder are printed to the terminal.
Reading a web page with jsoup
jsoup is a popular Java HTML parser.
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.23.2</version>
</dependency>
We use this Maven dependency.
package com.zetcode;
import org.jsoup.Jsoup;
import java.io.IOException;
public class ReadWebPageEx2 {
public static void main(String[] args) throws IOException {
String webPage = "https://example.com";
String html = Jsoup.connect(webPage).get().html();
System.out.println(html);
}
}
The code example uses jsoup to download and print a small web page.
String html = Jsoup.connect(webPage).get().html();
The connect method connects to the specified web page.
The get method issues a GET request. Finally, the
html method retrieves the HTML source.
Reading a web page with HtmlCleaner
HtmlCleaner is an open source HTML parser written in Java.
<dependency>
<groupId>net.sourceforge.htmlcleaner</groupId>
<artifactId>htmlcleaner</artifactId>
<version>2.29</version>
</dependency>
For this example, we use the htmlcleaner Maven dependency.
package com.zetcode;
import java.io.IOException;
import java.net.URL;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.SimpleHtmlSerializer;
import org.htmlcleaner.TagNode;
public class ReadWebPageEx3 {
public static void main(String[] args) throws IOException {
var url = new URL("https://example.com");
var props = new CleanerProperties();
props.setOmitXmlDeclaration(true);
var cleaner = new HtmlCleaner(props);
TagNode node = cleaner.clean(url);
var htmlSerializer = new SimpleHtmlSerializer(props);
htmlSerializer.writeToStream(node, System.out);
}
}
The example uses HtmlCleaner to download a web page.
var props = new CleanerProperties(); props.setOmitXmlDeclaration(true);
In the properties, we set to omit the XML declaration.
var htmlSerializer = new SimpleHtmlSerializer(props); htmlSerializer.writeToStream(node, System.out);
A SimpleHtmlSerializer creates the resulting HTML without
indentation or compacting.
Reading a web page with Apache HttpClient
Apache HttpClient is an HTTP/1.1-compliant HTTP client implementation. It can retrieve a web page using the request and response process. An HTTP client implements the client side of the HTTP and HTTPS protocols.
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.6.4</version>
</dependency>
We use this Maven dependency for the Apache HTTP client.
package com.zetcode;
import java.io.IOException;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.io.entity.EntityUtils;
public class ReadWebPageEx4 {
public static void main(String[] args) throws IOException {
var url = "https://example.com";
try (var client = HttpClients.createDefault()) {
var request = new HttpGet(url);
request.addHeader("User-Agent", "Apache HttpClient");
try (var response = client.execute(request)) {
var entity = response.getEntity();
var content = EntityUtils.toString(entity);
System.out.println(content);
}
}
}
}
In the code example, we send a GET HTTP request to the specified web page and receive an HTTP response. From the response, we read the HTML source.
var client = HttpClients.createDefault();
An HttpClient is built.
request = new HttpGet(url);
HttpGet is a class for the HTTP GET method.
request.addHeader("User-Agent", "Apache HttpClient");
var response = client.execute(request);
A GET request is executed, and an HTTP response is received.
var entity = response.getEntity(); var content = EntityUtils.toString(entity); System.out.println(content);
We retrieve the web page content from the response.
Reading a web page with Jetty HttpClient
The Jetty project provides an HTTP client as well.
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>12.1.13</version>
</dependency>
This is a Maven dependency for the Jetty HTTP client.
package com.zetcode;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.ContentResponse;
public class ReadWebPageEx5 {
public static void main(String[] args) throws Exception {
var client = new HttpClient();
try {
client.start();
var url = "https://example.com";
var res = client.GET(url);
System.out.println(res.getContentAsString());
} finally {
client.stop();
}
}
}
In the example, we get the HTML source of a web page with the Jetty HTTP client.
client = new HttpClient(); client.start();
An HttpClient is created and started.
ContentResponse res = client.GET(url);
A GET request is issued to the specified URL.
System.out.println(res.getContentAsString());
The content is retrieved from the response with the
getContentAsString method.
Reading a web page with HtmlUnit
HtmlUnit is a headless browser for testing web-based applications.
<dependency>
<groupId>org.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>5.5.0</version>
</dependency>
We use this Maven dependency to access HtmlUnit.
package com.zetcode;
import org.htmlunit.WebClient;
import org.htmlunit.WebResponse;
import org.htmlunit.html.HtmlPage;
import java.io.IOException;
public class ReadWebPageEx6 {
public static void main(String[] args) throws IOException {
try (var webClient = new WebClient()) {
var url = "https://example.com";
var page = webClient.getPage(url);
var response = page.getWebResponse();
var content = response.getContentAsString();
System.out.println(content);
}
}
}
The example downloads a web page and prints it using the HtmlUnit library.
Source
In this article, we read a web page in Java using various tools, including HttpClient, URL, jsoup, HtmlCleaner, Apache HttpClient, Jetty HttpClient, and HtmlUnit.
Author
List all Java tutorials.