Android上的Http摘要认证失败,401

时间:2014-12-14 19:13:02

标签: android digest-authentication http-digest

我正在尝试从Android客户端进行http摘要身份验证。这是一个成功连接和完成摘要的卷曲线。

curl -i --digest --user 'c@c.com:500a436e-2d5d-4a2e-be82-19651f7ea904' -v https://localhost:8080/v1/resources/debug

这是我的Android客户端中的一次尝试,返回401。

AndroidHttpClient httpClient = AndroidHttpClient.newInstance("user agent");

String url = "http://localhost:8080/v1/resources/debug";
URL urlObj = new URL(url);
HttpHost host = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol());
AuthScope scope = new AuthScope(urlObj.getHost(), urlObj.getPort());
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("c@c.com", "500a436e-2d5d-4a2e-be82-19651f7ea904");

CredentialsProvider cp = new BasicCredentialsProvider();
cp.setCredentials(scope, creds);
HttpContext credContext = new BasicHttpContext();
credContext.setAttribute(ClientContext.CREDS_PROVIDER, cp);

HttpGet job = new HttpGet(url);
HttpResponse response = httpClient.execute(host,job,credContext);
StatusLine status = response.getStatusLine();
System.out.println("#### " + status.toString());
httpClient.close();

这是客户端的第二次尝试,也返回401。

DefaultHttpClient httpclient = new DefaultHttpClient();
DefaultHttpClient httpclient2 = new DefaultHttpClient();
HttpGet httpget = new HttpGet("https://localhost:8080/v1/resources/debug");

HttpResponse response = httpclient.execute(httpget);
System.out.println(response.getStatusLine());

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
    Header authHeader = response.getFirstHeader(AUTH.WWW_AUTH);
    System.out.println("authHeader = " + authHeader);
    DigestScheme digestScheme = new DigestScheme(); 
    digestScheme.processChallenge(authHeader);   
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("c@c.com", "500a436e-2d5d-4a2e-be82-19651f7ea904
    httpget.addHeader(digestScheme.authenticate(creds, httpget));
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = httpclient2.execute(httpget, responseHandler);
    System.out.println("responseBody : " + responseBody);
}

任何人都可以看到我错过了什么才能使身份验证工作?我们知道它不是服务器问题,因为卷曲线很好。

0 个答案:

没有答案