⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e139. accessing a password-protected url.txt

📁 这里面包含了一百多个JAVA源文件
💻 TXT
字号:
// Install the custom authenticator
    Authenticator.setDefault(new MyAuthenticator());
    
    // Access the page
    try {
        // Create a URL for the desired page
        URL url = new URL("http://hostname:80/index.html");
    
        // Read all the text returned by the server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;
        while ((str = in.readLine()) != null) {
            // str is one line of text; readLine() strips the newline character(s)
        }
        in.close();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
    
    public class MyAuthenticator extends Authenticator {
        // This method is called when a password-protected URL is accessed
        protected PasswordAuthentication getPasswordAuthentication() {
            // Get information about the request
            String promptString = getRequestingPrompt();
            String hostname = getRequestingHost();
            InetAddress ipaddr = getRequestingSite();
            int port = getRequestingPort();
    
            // Get the username from the user...
            String username = "myusername";
    
            // Get the password from the user...
            String password = "mypassword";
    
            // Return the information
            return new PasswordAuthentication(username, password.toCharArray());
        }
    }

 Related Examples 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -