java.net.PasswordAuthentication.PasswordAuthentication(String userName, char[] password) : Authenticator « Network Protocol « Java






java.net.PasswordAuthentication.PasswordAuthentication(String userName, char[] password)

import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.Authenticator; import java.net.InetAddress; import java.net.PasswordAuthentication; import java.net.URL; publicclass Main { publicstaticvoid main(String[] argv) throws Exception { Authenticator.setDefault(new MyAuthenticator()); URL url = new URL("http://hostname:80/index.html");  BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String str; while ((str = in.readLine()) != null) { System.out.println(str); } in.close(); } } class MyAuthenticator extends Authenticator { protected PasswordAuthentication getPasswordAuthentication() { String promptString = getRequestingPrompt(); System.out.println(promptString); String hostname = getRequestingHost(); System.out.println(hostname); InetAddress ipaddr = getRequestingSite(); System.out.println(ipaddr); int port = getRequestingPort(); String username = "name"; String password = "password"; returnnew PasswordAuthentication(username, password.toCharArray()); } } 








Related examples in the same category

1.String java.net.Authenticator.getRequestingPrompt()
2.Authenticator.setDefault(new Authenticator());
3.String java.net.Authenticator.getRequestingHost()
4.InetAddress java.net.Authenticator.getRequestingSite()
5.int java.net.Authenticator.getRequestingPort()
6.Identify yourself using HTTP Authentification
close