Authenticator : Authenticator « Network « Java Tutorial






import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;

public class MainClass {

  public static void main(String args[]) throws Exception {

    Authenticator.setDefault(new DialogAuthenticator());

    URL u = new URL("secure url");
    InputStream in = u.openStream();
    in = new BufferedInputStream(in);
    Reader r = new InputStreamReader(in);
    int c;
    while ((c = r.read()) != -1) {
      System.out.print((char) c);
    }
  }

}

class DialogAuthenticator extends Authenticator {

  public DialogAuthenticator() {
  }

  public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("username", "password".toCharArray());
  }

}








19.21.Authenticator
19.21.1.Authenticator
19.21.2.Authenticator.setDefault(new Authenticator());
19.21.3.String java.net.Authenticator.getRequestingPrompt()
19.21.4.String java.net.Authenticator.getRequestingHost()
19.21.5.InetAddress java.net.Authenticator.getRequestingSite()
19.21.6.int java.net.Authenticator.getRequestingPort()