您用喜好的浏覽器在網上沖浪時
從浏覽器訪問一個諸如
在 Java
現在
所需步驟如下所示
第一步
Authenticator
第二步
class MyAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication (
}
}
以下程序完整地顯示了這種行為
import java
import
import java
import java
public class URLPassword extends Frame {
private TextField tf = new TextField();
private TextArea ta = new TextArea();
public URLPassword() {
super (
// 安裝 Authenticator
Authenticator
// 設置屏幕
add (tf
ta
add (ta
tf
public void actionPerformed (ActionEvent e) {
String s = tf
if (s
ta
}
});
addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
dispose();
System
}
});
}
private String fetchURL (String urlString) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
try {
URL url = new URL (urlString);
InputStream content = (InputStream)url
BufferedReader in =
new BufferedReader (new InputStreamReader (content));
String line;
while ((line = in
pw
}
} catch (MalformedURLException e) {
pw
} catch (IOException e) {
pw
}
return sw
}
public static void main (String args[]) {
Frame f = new URLPassword();
f
f
}
class MyAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
final Dialog jd = new Dialog (URLPassword
jd
Label jl = new Label (getRequestingPrompt());
jd
TextField username = new TextField();
username
jd
TextField password = new TextField();
password
password
jd
Button jb = new Button (
jd
jb
public void actionPerformed (ActionEvent e) {
jd
}
});
jd
jd
return new PasswordAuthentication (username
}
}
}
From:http://tw.wingwit.com/Article/program/Java/Javascript/201311/25368.html