// This example is from the book _Java in a Nutshell_ by David Flanagan. // Written by David Flanagan. Copyright (c) 1996 O'Reilly & Associates. // You may study, use, modify, and distribute this example for any purpose. // This example is provided WITHOUT WARRANTY either expressed or implied. import java.awt.*; import java.io.*; import java.net.*; // The fetch() method in this class only works for fetching text/plain // data, and a few standard image types. The standard Java distribution // doesn't contain content handlers for other types (such as text/html), // so this code exits with an exception. public class Fetch { // Get the contents of a URL and return it as a string. public static String fetch(String address) throws MalformedURLException, IOException { URL url = new URL(address); return (String) url.getContent(); } // Get the contents of a URL and return it as an image public static Image fetchimage(String address) throws MalformedURLException, IOException { URL url = new URL(address); return (Image) url.getContent(); } // Test out the fetch() method. public static void main(String[] args) throws MalformedURLException, IOException { System.out.println(fetch(args[0])); } }