Java 如果提供了不正确的登录凭据,如何避免Android中的httpsURLConnection.getInputStream()挂起?

Java 如果提供了不正确的登录凭据,如何避免Android中的httpsURLConnection.getInputStream()挂起?,java,android,httpurlconnection,Java,Android,Httpurlconnection,这里设置了身份验证凭据,如果提供的用户/密码正确,则一切正常,但如果用户/密码不正确,则挂起。这不是服务器问题,我用Curl和Browser检查过,不正确的凭证会立即返回401 Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthenti

这里设置了身份验证凭据,如果提供的用户/密码正确,则一切正常,但如果用户/密码不正确,则挂起。这不是服务器问题,我用Curl和Browser检查过,不正确的凭证会立即返回401

    Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, password.toCharArray());
        }
    });
挂起的代码就在这里,它挂起在这一行:in=new BufferedReader(new InputStreamReader(httpURLConn.getInputStream());(没有例外,它只是停留在这条线上)


可能是服务器使连接保持活动状态(Keep alive标头)?

否,响应中没有Keep alive标头
    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
                , "An exception occured while reading data from remote host. httpURLConn.responseCode = " + httpURLConn.getResponseCode()
                + " / httpURLConn.responseMessage = " + httpURLConn.getResponseMessage(), e);
        throw new IOException();
    }