2011-08-13 56 views
0

我做这样的事情: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()覆盖中;即使在由于凭证错误而导致多次失败之后,这种情况一遍又一遍地被调用。

+0

的可能重复[安卓与身份验证HttpsURLConnection的基本身份验证迭代永远当密码错误(在401响应)](HTTP:// stackoverflow.com/questions/7808301/android-httpsurlconnection-with-authenticator-for-basic-authentication-iterates) – yanchenko

回答

0

我处理这个连接/认证问题的方式是通过与周围他们:

try 
{ 
    //your connection attempt 
} 
catch (Exception e) 
{ 
//something goes wrong, handling the situation... for example : 
    error = true; 
} 
if (error) 
{ 
    //return control and notify the user, with a toast an alertDialog or whatever 
} 

这是丑陋的,但它通常工作......有人在这里肯定有一个更好的解决方案。

编辑: 也许这个链接将帮助:http://sites.google.com/site/androidhowto/password-auth-http

+0

我围绕着try/catch。连接似乎并没有超时。 – SK9

+0

查看我的帖子上的链接。 – mthpvg

+0

谢谢 - 这是我已经在做的一部分。我将连接时间从5秒减少到0.5秒,强制超时。我正在捕获这个异常,并且控制权应该回到用户身上。但是,如果用户提供错误的凭据,我想要做类似的事情 - 但我仍然不能。嗯。 – SK9