getPasswordAuthentication

getPasswordAuthentication

Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        4年前关闭。
                                                                                            
                
        
我是网络编程的新手。
这是我的代码:

public class ComputerNetworks extends Authenticator {
    private JDialog passwordDialog;
    private JLabel mainLabel= new JLabel("Please enter username and password: ");
    private JLabel userLabel = new JLabel("Username: ");
    private JLabel passwordLabel = new JLabel("Password: ");
    private JTextField usernameField = new JTextField(20);
    private JPasswordField passwordField = new JPasswordField(20);
    private JButton okButton = new JButton("OK");
    private JButton cancelButton = new JButton("Cancel");

    public ComputerNetworks( ) {
        this("", new JFrame());
    }

    public ComputerNetworks(String username) {
        this(username, new JFrame());
    }

    public ComputerNetworks(JFrame parent) {
        this("", parent);
    }

    public ComputerNetworks(String username, JFrame parent) {
        this.passwordDialog = new JDialog(parent, true);
        Container pane = passwordDialog.getContentPane( );
        pane.setLayout(new GridLayout(4, 1));
        pane.add(mainLabel);
        JPanel p2 = new JPanel( );
        p2.add(userLabel);
        p2.add(usernameField);
        usernameField.setText(username);
        pane.add(p2);
        JPanel p3 = new JPanel( );
        p3.add(passwordLabel);
        p3.add(passwordField);
        pane.add(p3);
        JPanel p4 = new JPanel( );
        p4.add(okButton);
        p4.add(cancelButton);
        pane.add(p4);
        ActionListener al = new OKResponse( );
        okButton.addActionListener(al);
        usernameField.addActionListener(al);
        passwordField.addActionListener(al);
        cancelButton.addActionListener(new CancelResponse( ));
        passwordDialog.pack( );
        passwordDialog.setVisible(true);

    }

    private void show( ) {
        String prompt = this.getRequestingPrompt( );
        if (prompt == null) {
            String site = this.getRequestingSite().getHostName( );
            String protocol = this.getRequestingProtocol( );
            int port = this.getRequestingPort();
            if (site != null & protocol != null) {
                prompt = protocol + "://" + site;
                if (port > 0)
                    prompt += ":" + port;
            } else {
                prompt = "";
            }
        }
        mainLabel.setText("Please enter username and password for "
            + prompt + ": ");
        passwordDialog.pack();
        passwordDialog.setVisible(true);
    }


    PasswordAuthentication response = null;

    class OKResponse implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("OK clicked");
            passwordDialog.setVisible(false);
            // The password is returned as an array of
            // chars for security reasons.
            char[] password = passwordField.getPassword( );
            String username = usernameField.getText( );
            // Erase the password in case this is used again.
            passwordField.setText("");
            response = new PasswordAuthentication(username, password);
        }
    }

    class CancelResponse implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            passwordDialog.hide( );
            // Erase the password in case this is used again.
            passwordField.setText("");
            response = null;
        }
    }

    public PasswordAuthentication getPasswordAuthentication( ) {
        this.show();
        return this.response;
    }

    public static void main(String[] args) throws Exception {
        Authenticator.setDefault(new ComputerNetworks());
        URL u = new URL("http://www.google.co.in");
        InputStream in = u.openStream();
    }
}


我正在从IDE运行此代码。问题是虽然我打开了带有URL的流,但没有调用getPasswordAuthentication()Authenticator方法。
请帮忙。
提前致谢

最佳答案

JavaDoc


  getPasswordAuthentication()
  
  protected PasswordAuthentication getPasswordAuthentication()
  需要密码授权时调用。子类应覆盖默认实现,该实现返回null。
  返回值:
  从用户收集的PasswordAuthentication,如果未提供,则为null。


就我所知,google.co.in不需要密码授权。因此,不使用Authenticator。尝试输入确实需要身份验证的URL,然后查看是否有帮助。

09-29 21:52