Online Java Compiler By
JavaTpoint.com
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.Authenticator; import java.net.InetAddress; import java.net.PasswordAuthentication; import java.net.URL; public class AuthenticatorgetPasswordAuthenticationExample2 { public static void main(String[] argv) throws Exception { passwordvalidator obj = new passwordvalidator(); Authenticator.setDefault(new passwordvalidator()); URL url = new URL("https://www.javaTpoint.com"); obj.getPasswordAuthentication(); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String str; while ((str = in.readLine()) != null) { System.out.println(str); } in.close(); } } class Passwordvalidator extends Authenticator { protected PasswordAuthentication getPasswordAuthentication() { // return null because we havent set the server String promptString = this.getRequestingPrompt(); System.out.println(promptString); String hostname = this.getRequestingHost(); System.out.println(hostname); String username = "name"; String password = "password"; return new PasswordAuthentication(username, password.toCharArray()); } }
Output