我正在学习在 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
这种方法是错误的吗?非常感谢您的宝贵时间。