Αίτημα HTTP στη Java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.MalformedURLException;

public class UrlRetriever {
    public static void main(String[] args) {
	if (args.length != 1) {
	    System.err.println("Usage: UrlRetriever URL");
	    System.exit(1);
	}

        URL url = null;
        try {
            // Make a request to the specified URL
            url = new URL(args[0]);
        } catch (MalformedURLException e) {
                System.err.println("Invalid URL: " + e);
                System.exit(1);
        }

        HttpURLConnection connection = null;
        try {
            connection = (HttpURLConnection)url.openConnection();
        } catch (ClassCastException e) {
            System.err.println("Specified protocol is not HTTP");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Connection error: " + e);
            System.exit(1);
        }

        try {
            connection.setRequestMethod("GET");

            // Get the response from the server
            int status = connection.getResponseCode();
            BufferedReader in = new BufferedReader(
                new InputStreamReader(connection.getInputStream()));
            int c;
            StringBuilder content = new StringBuilder();
            while ((c = in.read()) != -1) {
                content.append((char)c);
            }
            in.close();

            // Print the response
            System.out.println(content.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}