如果提供了错误的登录凭据,如何避免在Android中挂起httpsURLConnection.getInputStream()?

如果在此处设置了身份验证凭据,如果提供的用户/密码正确,则一切正常,但如果它们不正确则会挂起.这不是服务器问题,我使用Curl和浏览器检查,不正确的凭据立即返回401:
Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getpasswordAuthentication() {
            return new PasswordAuthentication(user,password.tochararray());
        }
    });

挂起的代码在这里,它挂起在这一行:in = new BufferedReader(new InputStreamReader(httpURLConn.getInputStream())); (没有例外,它只停留在这条线上)

try {
        URL url = new URL(resourceUrl);
        HttpURLConnection httpURLConn = (HttpURLConnection) url.openConnection();
        String rawData = "";
        String currentLine = null;
        BufferedReader in = null;

        in = new BufferedReader(new InputStreamReader(httpURLConn.getInputStream()));
        while ((currentLine = in.readLine()) != null) {
            rawData = rawData.concat(currentLine);
        }
        in.close();
    } catch (UnkNownHostException e) {
        Log.i(CLASS_NAME + "::" + METHOD_NAME,"An exception occured while reading data from remote host. httpURLConn.responseCode = " + httpURLConn.getResponseCode()
                + " / httpURLConn.responseMessage = " + httpURLConn.getResponseMessage(),e);
        throw new UnkNownHostException();
    } catch (IOException e) {
        Log.i(CLASS_NAME + "::" + METHOD_NAME,e);
        throw new IOException();
    }

解决方法

可能是服务器保持连接活动(Keep-Alive标头)?

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...