Online Java Compiler By
JavaTpoint.com
import java.util.concurrent.BlockingQueue; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Authenticator; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.PasswordAuthentication; import java.net.URL; public class AuthenticatorgetPasswordAuthenticationExample1{ public static void main(String[] args) { try { passwordvalidator obj = new passwordvalidator(); Authenticator.setDefault(new passwordvalidator()); URL url = new URL("https://www.javatpoint.com"); // calling password validator obj.getPasswordAuthentication(); BufferedReader inb = new BufferedReader(new InputStreamReader(url.openStream())); String line; while ((line = inb.readLine()) != null) { System.out.println(line); } inb.close(); } catch (MalformedURLException e) { System.out.println("Malformed URL: " + e.getMessage()); } catch (IOException e) { System.out.println("I/O Error: " + e.getMessage()); } } public static class PasswordValidator extends Authenticator { @Override protected PasswordAuthentication getPasswordAuthentication() { System.out.println(" password validating..."); String username = "javaTpoint"; String password = "12345"; return new PasswordAuthentication(username, password.toCharArray()); } } }
Output