我正在尝试访问需要通过用户名和密码进行身份验证的网址.
建设时没有错误..我错过了什么吗?
这是第一个类,它设置将由网络代码使用的验证器
public class AccessPasswordProtectedURLWithAuthenticator {
public static void main(String[] args) {
try {
// when a proxy or an HTTP server asks for authentication.
Authenticator.setDefault(new Authenticator(){});
URL url = new URL("http://website.html");
// read text returned by server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
} catch (MalformedURLException e) {
System.out.println("Malformed URL: " + e.getMessage());
} catch (IOException e) {
System.out.println("I/O Error: " + e.getMessage());
}
}
} …Run Code Online (Sandbox Code Playgroud)