This problem occurs when I log in and register. Android client and Django background. The above error occurs every time I enter the wrong password, but the correct username and password are correct. There is another strange phenomenon: When I write a Java test code, inputting the wrong user name and password will not throw this exception, And I transfer the code to the android program, this exception occurs. Real and confusing ....
After checking the source code, we found that httpurlconnectionimpl. Java uses httpurlconnection to request HTTP. This exception is thrown in two statuses:
/** * React to a failed authorization response by looking up new credentials. */ private Retry processAuthHeader(String responseHeader, String retryHeader) throws IOException { // keep asking for username/password until authorized String challenge = this.responseHeader.get(responseHeader); if (challenge == null) { throw new IOException("Received authentication challenge is null"); } String credentials = getAuthorizationCredentials(challenge); if (credentials == null) { return Retry.NONE; // could not find credentials, end request cycle } // add authorization credentials, bypassing the already-connected check requestHeader.set(retryHeader, credentials); return Retry.SAME_CONNECTION; }
/** * Returns the retry action to take for the current response headers. The * headers, proxy and target URL or this connection may be adjusted to * prepare for a follow up request. */ private Retry processResponseHeaders() throws IOException { switch (responseCode) { case HTTP_PROXY_AUTH: // proxy authorization failed ? if (!usingProxy()) { throw new IOException( "Received HTTP_PROXY_AUTH (407) code while not using proxy"); } return processAuthHeader("Proxy-Authenticate", "Proxy-Authorization"); case HTTP_UNAUTHORIZED: // HTTP authorization failed ? return processAuthHeader("WWW-Authenticate", "Authorization"); case HTTP_MULT_CHOICE: case HTTP_MOVED_PERM: case HTTP_MOVED_TEMP: case HTTP_SEE_OTHER: case HTTP_USE_PROXY: if (!getInstanceFollowRedirects()) { return Retry.NONE; } if (requestBodyOut != null) { // TODO: follow redirects for retryable output streams... return Retry.NONE; } if (++redirectionCount > MAX_REDIRECTS) { throw new ProtocolException("Too many redirects"); } String location = getHeaderField("Location"); if (location == null) { return Retry.NONE; } if (responseCode == HTTP_USE_PROXY) { int start = 0; if (location.startsWith(url.getProtocol() + ':')) { start = url.getProtocol().length() + 1; } if (location.startsWith("//", start)) { start += 2; } setProxy(location.substring(start)); return Retry.NEW_CONNECTION; } URL previousUrl = url; url = new URL(previousUrl, location); if (!previousUrl.getProtocol().equals(url.getProtocol())) { return Retry.NONE; // the scheme changed; don't retry. } if (previousUrl.getHost().equals(url.getHost()) && previousUrl.getEffectivePort() == url.getEffectivePort()) { return Retry.SAME_CONNECTION; } else { // TODO: strip cookies? requestHeader.removeAll("Host"); return Retry.NEW_CONNECTION; } default: return Retry.NONE; } }
Here, we also indirectly describe a precondition that error 401 is returned when a request fails. Therefore, you must add "www-authenticate" to the response header to solve this problem.