import java.net.*;
import java.util.*;
import java.io.*;
import javax.commerce.util.*;
/***
 * A tiny web page getter going thru a proxy server
 * using the BASE64Encoder from Sun's commerce package
 * This grabs the HTML page from a web server and prints it to std out
 * @author Mitch Fincher , 2003
**/
public class GetWebPageThruProxy {
    String proxyHost = "";
    String proxyPort = "";
    String proxyUsername = "";
    String proxyPassword = "";

public GetWebPageThruProxy( String proxyHost, String proxyPort, String proxyUsername, String proxyPassword) {
    this.proxyHost = proxyHost;
    this.proxyPort = proxyPort;
    this.proxyUsername = proxyUsername;
    this.proxyPassword = proxyPassword;
    System.setProperty("http.proxyHost",proxyHost );
    System.setProperty("http.proxyPort", proxyPort);
}
    
    public String getPage(String urlString) {
	URLConnection uc = null;
	StringBuffer sb = new StringBuffer();
	String line = null;
	BufferedReader bufferedReader = null;
	try {
	    uc = new URL(urlString).openConnection();
	    String whoami = new BASE64Encoder().encodeBuffer((proxyUsername+":"+proxyPassword).getBytes());
	    uc.setRequestProperty( "Proxy-Authorization", "basic "+whoami);
	    bufferedReader = new BufferedReader(new InputStreamReader(uc.getInputStream()));
	    while((line = bufferedReader.readLine()) != null) {
		sb.append(line);
	    }
	} catch (Exception ex)
	    { 
		System.out.println("Exception thrown: " + ex);
		ex.printStackTrace(System.out); 
	    } finally {
		if(bufferedReader != null) {
		    try { 
			bufferedReader.close();
		    } catch(Exception ignore){}
		}
	    }

	return sb.toString();
    }
    

    public static void main(String argv[])
    {
	if(argv.length < 5) {
	    System.out.println("usage: GetWebPage proxyHost proxyPort proxyUsername proxyPassword url");
	    System.exit(1);
	}
	String proxyHost = argv[0];
	String proxyPort = argv[1];
	String proxyUsername = argv[2];
	String proxyPassword = argv[3];
	String urlString = argv[4];
	GetWebPageThruProxy getWebPageThruProxy = new GetWebPageThruProxy(proxyHost,proxyPort,proxyUsername,proxyPassword);
	String temp = getWebPageThruProxy.getPage(urlString);
	System.out.println(temp);
    }
} // class GetWebPage
