我做这样的事情:Android和应对恶劣认证证书
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(10*1000);
urlConnection.setReadTimeout(10*1000);
urlConnection.setUseCaches(false);
DataInputStream dis = new DataInputStream(urlConnection.getInputStream());
在url
收到一个JSON字符串。
当我输入验证凭证是否正确,并与
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(loginNameString, passwordString.toCharArray());
}
});
设定。但是这工作得很好,当我故意提供不正确的凭据当然请求失败,但是该应用程序最终只是超时;在哪里以及如何处理此事件以通知用户并将控制权交还给她 - 是否存在钩子?
** 编辑 - 我把System.out.println("hello!");
放在我的getPasswordAuthentication()
覆盖中;即使在由于凭证错误而导致多次失败之后,这种情况一遍又一遍地被调用。
的可能重复[安卓与身份验证HttpsURLConnection的基本身份验证迭代永远当密码错误(在401响应)](HTTP:// stackoverflow.com/questions/7808301/android-httpsurlconnection-with-authenticator-for-basic-authentication-iterates) – yanchenko