import java.io.*;
import java.net.URL;
import java.net.MalformedURLException;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.log.*;
import org.apache.commons.httpclient.methods.*;

import org.apache.log4j.BasicConfigurator;

/**
 * Retrieve the contents from the given URL
 *
 */
public class GetPage
{
    public static void main(String args[])
    {
        URL url = null;
        UsernamePasswordCredentials creds = null;


        // Create URL object from the command line arg
        try{
            BasicConfigurator.configure();

            System.out.println("Getting page for " + args[0]);
            url = new URL(args[0]);

            // Create the client for the URL
            HttpMultiClient client = new HttpMultiClient();

            UrlGetMethod get = new UrlGetMethod();
            get.setUrl(args[0]);

            client.executeMethod(get);
            int statusCode = get.getStatusCode();
            String statusText = get.getStatusText();

            System.out.println("Result:  " + statusCode + " " + statusText);

            Header headers[] = get.getResponseHeaders();
            for(int i=0;i<headers.length;i++){
                System.out.print(headers[i].toString());
            }
        }catch(MalformedURLException e){
            System.err.println(e.toString());
            return;
        }catch(HttpException e){
            System.err.println("GetPage:  "  + e.toString());
            e.printStackTrace();
        }catch(IOException e){
            System.err.println(e.toString());
        }

    }
}

