received authentication challenge is null

來源:互聯網
上載者:User

  這個問題是我在登入註冊的時候出現的。Android用戶端與DJANGO後台。我每次輸入錯誤密碼的時候就會出現上述錯誤,而輸入正確的使用者名稱密碼是沒有這個錯誤的。還有一個奇怪的現象:我寫一個java測試代碼的時候,輸入錯誤的使用者名稱和密碼是不會拋出這個異常的,而我把該代碼轉移到Android程式裡面,就會出現這個異常。真實讓人費解。。。。

  查了下源碼發現HttpURLConnectionImpl.java,使用httpurlconnection請求http,有兩種狀態下會拋出這個異常:

/**     * 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;        }    }

  說到這裡,也間接說明一個前提條件,請求失敗返回的是401錯誤,故回應標頭必須添加上”WWW-Authenticate“即可解決這個問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.