【发布时间】:2011-10-27 00:49:44
【问题描述】:
我正在执行一个 GET 请求:
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setConnectTimeout(CONNECT_TIMEOUT);
urlConnection.setReadTimeout(READ_TIMEOUT);
urlConnection.connect();
凭证已设置的时间:
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(loginNameString, passwordString.toCharArray());
}
});
当我提供良好的凭据(即用户名和密码)时,一切正常。
提供错误凭据时,连接会发生什么情况?例如,如果我故意提供错误的凭据,并且在urlConnection.connect() 之后立即调用urlConnection.getResponseCode(),我的应用程序最终会超时,我必须强制关闭。这是为什么呢?
** 编辑。据我所知,当凭据错误时,HttpURLConnection 只是继续尝试连接(即使超时时间有限)。 (我知道这一点是因为我在返回之前在我的getPasswordAuthentication() 方法中添加了Log.v("Authentication", "Calling..."); 行)。如果凭据错误导致连接失败,我希望连接停止尝试!
【问题讨论】: