Java by API/java.net/Authenticator — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:43, 31 мая 2010
Authenticator: setDefault(Authenticator a)
import java.io.DataInputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.Properties;
public class Main {
public static void main(String[] argv) throws Exception {
byte[] b = new byte[1];
Properties systemSettings = System.getProperties();
systemSettings.put("http.proxyHost", "proxy.mydomain.local");
systemSettings.put("http.proxyPort", "80");
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("mydomain\\username", "password".toCharArray());
}
});
URL u = new URL("http://www.google.ru");
HttpURLConnection con = (HttpURLConnection) u.openConnection();
DataInputStream di = new DataInputStream(con.getInputStream());
while (-1 != di.read(b, 0, 1)) {
System.out.print(new String(b));
}
}
}
extends Authenticator
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());
}
}