Java Tutorial/Network/Authenticator
Содержание
Authenticator
<source lang="java">
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()); }
}</source>
Authenticator.setDefault(new Authenticator());
<source lang="java">
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 Main {
public static void 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"; return new PasswordAuthentication(username, password.toCharArray()); }
}</source>
InetAddress java.net.Authenticator.getRequestingSite()
<source lang="java">
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 Main {
public static void 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"; return new PasswordAuthentication(username, password.toCharArray()); }
}</source>
int java.net.Authenticator.getRequestingPort()
<source lang="java">
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 Main {
public static void 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"; return new PasswordAuthentication(username, password.toCharArray()); }
}</source>
String java.net.Authenticator.getRequestingHost()
<source lang="java">
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 Main {
public static void 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"; return new PasswordAuthentication(username, password.toCharArray()); }
}</source>
String java.net.Authenticator.getRequestingPrompt()
<source lang="java">
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 Main {
public static void 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"; return new PasswordAuthentication(username, password.toCharArray()); }
}</source>