import java.io.*;
import java.net.*;

public class TestAuth {

    public static void main (String[] args) {

	// Install the custom authenticator
  	Authenticator.setDefault (new MyAuthenticator());
    
	// Access the page
	try {
	    // Create a URL for the desired page
	    URL url = new URL ("http://localhost:9999/~senger/secure/a.tmp");
    
	    // Read all the text returned by the server
	    BufferedReader in = new BufferedReader (new InputStreamReader (url.openStream()));
	    String str;
	    while ((str = in.readLine()) != null) {
		System.out.println (str);
	    }
	    in.close();
	} catch (MalformedURLException e) {
	    System.err.println ("ERROR: " + e.toString());
	} catch (IOException e) {
	    System.err.println ("ERROR: " + e.toString());
	}
    }

    static class MyAuthenticator extends Authenticator {
        // This method is called when a password-protected URL is accessed
        protected PasswordAuthentication getPasswordAuthentication() {
            // Get information about the request
//             String promptString = getRequestingPrompt();
//             String hostname = getRequestingHost();
//             InetAddress ipaddr = getRequestingSite();
//             int port = getRequestingPort();
    
            // Get the username from the user...
            String username = "senger";
    
            // Get the password from the user...
            String password = "martin";
    
            // Return the information
            return new PasswordAuthentication (username, password.toCharArray());
        }
    }
}
