GET any URL in any website from a JAVA Program

This Java sample program shows how to GET content from any  HTTP URL from a Java porgram.

This is a very handy Java program that can be used from the command prompt to retrieve the HTML content returned by any website. I often use this code for screen scrapping. Once the content is retrieved, the HTML can be processed for screen scrapping.

/*
 *
 * A free Java sample program 
 * to GET any URL from any website
 *
 * free for use as long as this comment is included 
 * in the program as it is
 * 
 * More Free Java programs available for download 
 * at http://www.java-samples.com
 *
 */
import java.net.*;
import java.io.*;
import java.util.*; 
public class getme
{
    public static void main(String args[]) throws Exception
    {
        try
        {
            if (args.length != 1)
            {
                 System.err.println ("Invalid command parameters");
                 System.exit(0);
            }
// Change the following settings to suit your network's proxy settings.
	Properties sysProperties = System.getProperties();
	sysProperties.put("proxyHost", "proxy.cyberway.com.sg");
	sysProperties.put("proxyPort", "8080");
	sysProperties.put("proxySet",  "true");
            URL url = new URL(args[0]);
            InputStream in = url.openStream();
            BufferedInputStream bufIn = new BufferedInputStream(in);
            for (;;)
            {
                int data = bufIn.read();
                if (data == -1)
                    break;
                else
                    System.out.print ( (char) data);
            }
        }
        catch (MalformedURLException mue)
        {
            System.err.println ("Invalid URL");
        }
        catch (IOException ioe)
        {
            System.err.println ("I/O Error - " + ioe);
        }
    }
}

More Free Java Sample Code