URL

The Uniform Resource Locator (URL) locates all of the resources of the Net.

A URL has four components:

  • the protocol, separated from the rest of the locator by a colon (:). Common protocols are HTTP, FTP, gopher, and file.
  • the host name or IP address, this is delimited on the left by double slashes (//) and on the right by a slash (/) or optionally a colon (:).
  • the port number, an optional parameter, delimited on the left from the host name by a colon (:) and on the right by a slash (/). It defaults to port 80, the predefined HTTP port.
  • file path.

Java's URL class has several constructors:

URL(String urlSpecifier) throws MalformedURLException
create URL from string
URL(String protocolName, String hostName, int port, String path) throws MalformedURLException
break up the URL into its component parts
URL(String protocolName, String hostName, String path) throws MalformedURLException
break up the URL into its component parts
URL(URL urlObj, String urlSpecifier) throws MalformedURLException
use an existing URL as a reference context and then create a new URL from that context.

To access the actual content, create a URLConnection object from it, using its openConnection( ) method, like this: openConnection( ) has the following general form:


URLConnection openConnection( ) throws IOException

It returns a URLConnection object associated with the invoking URL object.

URL and URLConnection

Outputting the contents of the resource identified via a URL command-line argument


import java.io.InputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

class Main {

    public static void main(String[] args) throws Exception {

        URL url = new URL("http://google.com");
        try (InputStream is = url.openStream()) {
            int ch;
            while ((ch = is.read()) != -1) {
                System.out.print((char) ch);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Home 
  Java Book 
    Networking  

URL:
  1. URL
  2. new URL(String spec) throws MalformedURLException
  3. URL: getAuthority()
  4. URL: openConnection()
  5. URL: openStream()