Android 2.2中的DigestAuth问题

时间:2012-11-06 20:45:51

标签: android digest-authentication

我正在学习在Android中实现DigestAuthentification。到目前为止,我可以理解,因为我发现它很容易..Basic Auth。但我对Digest有麻烦。下面是我的代码片段,...数据(它应该是正确的)不是真的,因为我有兴趣知道逻辑我搞砸了:

    String challenge = "Digest username=\"username\", realm=\"django-tastypie\", nonce=\"1905145252.99:023A:1ec223524356154ca9f469f8e466919\", " +
                "uri=\"/login/\", algorithm=MD5, response=\"e9a49cffjfgj2d46gfjhgfhfee352452345\", opaque=\"726da4e9sdgsaha12bf4gadgad4636731b\", " +
                "qop=auth, nc=00000001, cnonce=\"082cdsfa875dcb2dca740\"";

        Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge);
        HttpRequest request = new BasicHttpRequest("Simple",
                "/login/");
        Credentials cred = new UsernamePasswordCredentials("username", "password");
        AuthScheme authscheme = new DigestScheme();
        authscheme.processChallenge(authChallenge);
        Header authResponse = authscheme.authenticate(cred, request);
        System.out.println("===> text: " + authResponse);

HttpGet httpGet = new HttpGet(
                "http://something/api/login/");
        httpGet.addHeader(authResponse);
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse httpResponse = httpClient.execute(httpGet);
            System.out.println("httpResponse: " + httpResponse.getStatusLine());

            HttpEntity responseEntity = httpResponse.getEntity();
            InputStream content = responseEntity.getContent();
            String result = convertStreamToString(content);
            System.out.println("content result:  " + result);
            JSONObject json = new JSONObject(result);
            System.out.println("json:  " + json);
            content.close();

        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

我一直在:httpResponse: HTTP/1.1 401 UNAUTHORIZED

这种方法有误吗?非常感谢你的时间。

0 个答案:

没有答案