Online Java Compiler By
JavaTpoint.com
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 AuthenticatorgetRequestingProtocolExample2 { public static void main(String[] args) { try { ClassAuthenticator obj1 =new ClassAuthenticator(); Authenticator.setDefault(new ClassAuthenticator()); obj1.getPasswordAuthentication() ; URL url = new URL("https://www.javaTpoint.com"); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); System.out.println("Protocol Used: " +url.getProtocol()); String line; while ((line = in.readLine()) != null) { System.out.println(line); } in.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 ClassAuthenticator extends Authenticator { protected PasswordAuthentication getPasswordAuthentication() { String name = getRequestingProtocol(); System.out.println(" " +name ); String username = "javaTpoint"; String password = "tutorial"; return new PasswordAuthentication(username, password.toCharArray()); } } }
Output