import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import java.net.HttpURLConnection; public class URLGet { public static void main(String[] args) throws IOException { // get command line arguments if (args.length != 1) { System.out.println("usage: java URLGet URL"); System.exit(0); } // open connection URL u = new URL(args[0]); URLConnection connection = u.openConnection(); // check whether response code is HTTP_OK (200) HttpURLConnection httpConnection = (HttpURLConnection)connection; int code = httpConnection.getResponseCode(); if (code != HttpURLConnection.HTTP_OK) { String message = httpConnection.getResponseMessage(); System.out.println(code + " " + message); return; } // read server response InputStream in = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String input; while ((input = reader.readLine()) != null) System.out.println(input); // close connection // connection.close(); } }