How to get URL content in Java

Reading from a URL in Java is a straightforward process similar to reading from an input stream. The term URL stands for Uniform Resource Locator, which refers to the address of a resource on the Internet. Java programs that interact with the Internet can utilize URLs to locate and access specific resources. In the java.net package, there is a URL class available that allows Java programs to represent a URL address.

URL

A URL consists of two essential components: the protocol required to access the resource and the location of the resource itself. To create a URL object in Java, the most convenient approach is to use a String that represents the human-readable form of the URL address. This String will contain the necessary information to identify the protocol and location of the desired resource.

By utilizing the URL class in Java, programmers can easily access resources on the Internet by reading from the URL object as if it were an input stream. This enables Java programs to retrieve data from web pages, download files, and interact with various online resources effortlessly.

URL url = new URL("https://net-informations.com/");

Steps for reading URL Content from webserver:

  1. Create a URL object from the String representation.
  2. Create a new BufferedReader, using a new InputStreamReader with the URL input stream.
  3. Read the text, using readLine() API method of BufferedReader.
import java.net.*; import java.io.*; public class TestClass { public static void main(String[] args) throws Exception { try{ URL url = new URL("https://net-informations.com/"); BufferedReader reader = new BufferedReader( new InputStreamReader(url.openStream())); String line; while ((line = reader.readLine()) != null) System.out.println(line); reader.close(); }catch(Exception ex){ System.out.println(ex); } } }

When executing the program, it is expected that the command window will display the HTML commands and textual content retrieved from the specified URL, "https://net-informations.com/". However, in some cases, an error message may appear instead.

IOException: java.net.UnknownHostException: www.yahoo.com

You can get the content of a URL using the java.net.URL and java.net.HttpURLConnection classes. First, create a URL object with the desired URL, then open a connection to the URL using openConnection(). Finally, read the content using an input stream from the connection and process the data as needed.