Auth

// Install the custom authenticator
enticator.setDefault(new Myenticator());

// Access the page
try {
    // Create a URL for the desired page
    URL url = new URL("http://hostname:80/index.html");

    // Read all the text returned by the server
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;
    while ((str = in.readLine()) != null) {
        // str is one line of text; readLine() strips the newline character(s)
    }
    in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}

public class Myenticator extends enticator {
    // This method is called when a password-protected URL is accessed
    protected Passwordentication getPasswordentication() {
        // 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 = "myusername";

        // Get the password from the user...
        String password = "mypassword";

        // Return the information
        return new Passwordentication(username, password.toCharArray());
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.