实现口令验证程序
我的程序如下:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*; 
public class URLPassword extends Frame{
private TextField tf = new TextField("请输入url地址");
private TextArea ta = new TextArea();
    
    public static void main(String[] args) {
       
     // TODO, add your application code
         Frame f=new URLPassword();
     f.setSize(300,300);
     f.setVisible(true);
    } /**
 * Method URLPassword
 *
 *
 */
public URLPassword() {
// TODO: Add your code here
super("url passwod test");


Authenticator.setDefault(new MyAuthenticator());
add(tf,BorderLayout.NORTH);
ta.setEditable(false);
tf.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
String s=tf.getText();
if(s.length()!=0)

{ta.setText(fetchURL(s));}
}
}
);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
                               dispose();
                                  System.exit(0);
                                  }
                                   });


} /**
 * Method URLPassword
 *
 *
 * @param urlString
 *
 * @return
 *
 */
private String fetchURL(String urlString) { 
// TODO: Add your code here
StringWriter sw =new StringWriter();
PrintWriter pw=new  PrintWriter(sw);
try{

URL url=new URL(urlString);
InputStream content=(InputStream)url.getContent();
BufferedReader in=new BufferedReader(new InputStreamReader(content) );
String line;
while((line=in.readLine())!=null)
{
pw.println(line);
}
}
catch(MalformedURLException e){
pw.println("invalid url");
}
catch(IOException e) {
pw.println("Error reading  url");
}
return sw.toString();

}}class MyAuthenticator extends Authenticator {

/**
 * Method getPasswordAuthentication
 *
 *
 * @return
 *
 */
protected PasswordAuthentication getPasswordAuthentication() {
// TODO: Add your code here
    final Dialog jd=new Dialog(URLPassword.this,"Please input Password",true);
jd.setLayout(new GridLayout(0,1));
Label jl=new Label(getRequestingPrompt());
jd.add(jl);
TextField username =new TextField();
username.setBackground(Color.lightGray);
jd.add(username);


TextField password=new TextField();
password.setEchoChar('*');
password.setBackground(Color.lightGray);
jd.add(password);


Button jb=new Button("Yes");
jd.add(jb);


jb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
                                   jd.dispose();
                                               }
                                               }
                                               );
     jd.pack();
     jd.setVisible(true);
return  new PasswordAuthentication(username.getText(),password.getText().toCharArray());


}
}
结果输出如下:
C:\Program Files\Xinox Software\JCreatorV4\MyProjects\URLPassword\src\URLPassword.java:108: not an enclosing class: java.net.PasswordAuthentication
        final Dialog jd=new Dialog(PasswordAuthentication.this,"Please input Password",true);
                                                             ^
1 errorProcess completed.

解决方案 »

  1.   


    //URLPassword.this?? 语法错误
    final Dialog jd=new Dialog(URLPassword.this,"Please input Password",true);//可以修改成在URLPassword中调用时传递一个参数
    //构造函数URLPassword中,生成MyAuthenticator对象时传入对象引用
    Authenticator.setDefault(new MyAuthenticator(this));//为MyAuthenticator增加构造函数,保存传入的URLPassword的引用
    class MyAuthenticator extends Authenticator {
       private Frame frame; //增加一个Frame对象引用
       public MyAuthenticator(Frame frame) {
          super();
          this.frame = frame;
          System.out.println("MyAuthenticator done");
       }
       /**
        * Method getPasswordAuthentication
        * @return
        */
       protected PasswordAuthentication getPasswordAuthentication() {
          final Dialog jd=new Dialog(frame,"Please input password",true);
          ...
       }
    }是可以运行了,也可以获取到网站的数据了(html格式),但好像没有提示输入密码啊,这部分该怎么调用现在还不会;楼主调出来了也分享下呀~