我使用Authenticator像这样登录到服务器:
Authenticator.setDefault (new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication ("admin", "*****".toCharArray());
}
});
很好。但是,我如何确保授权实际上是成功的呢?
Authenticator类表示一个对象,该对象知道如何获得网络连接的身份验证。通常,它将通过提示用户输入信息来完成此操作。
到目前为止,您所做的就是向系统注册一个匿名类的实例,现在您要做的就是定义一个URL并连接到它。URL url = new URL("http://192.168.1.1/");// Connecting to this URL requires username and password to be entered.
但是我不认为有一种常见的方法来检查登录是否成功,我认为你必须读取解析http响应并尝试从中提取有用的信息,例如
if (response.contains("HREF=css/login.css"))
//login failed