/*
* Connect to a URL and print the retrieved bytes.
* Run by java -jar UrlTester <httpSite>
*/
package urltester;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
/**
*
* @author jbrown
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
String url = null;
if (args.length > 0) {
url = args[0];
} else {
System.out.println("Url is required.");
System.out.println("Syntax: ");
System.out.println(" jar -jar urltester.jar <url>");
System.exit(1);
}
URL site;
InputStream in = null;
System.out.println("Connecting to " + url);
try {
site = new URL(url);
in = site.openStream();
byte[] buffer = new byte[8192];
while (true) {
int bytes = in.read(buffer);
if (bytes < 0) {
break;
}
System.out.write(buffer, 0, bytes);
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException iOException) {
// punt.
}
}
}
}
}