GetWebPage is a simple Java program to connect to a URL and output the contents. It shows how to use java.net.URL to connect to a website and retrieve the contents of a page.
//
// GetWebPage.java
// SimpleNetworking
//
// Created by Mark W. Shead on 12/4/04.
//
// A simple Application to get the contents of a web page
// using java's built in classes.
// Usage: java GetWebPage http://www.website.com/page.html
import java.io.*;
import java.net.*;
public class GetWebPage {
public static void main (String argv[]) {
try {
//Create a URL
URL url = new URL(argv[0]);
//Open a stream to the URL
BufferedInputStream bis =
new BufferedInputStream(url.openStream());
//Read each character and print it out.
int c = bis.read();
while (c >=0) {
System.out.print((char)c);
c = bis.read();
}
} catch (Exception e) {
System.out.println(e + "\n");
System.out.println("Usage: java GetWebPage "
+ " http://www.website.com/page.html");
System.exit(0);
}
}
}