【问题标题】:Apache HttpClient Digest Authentication FailedApache HttpClient 摘要身份验证失败
【发布时间】:2012-09-27 23:08:57
【问题描述】:

我正在尝试使用 HttpClient 库执行摘要式身份验证,但我不断收到:HTTP/1.1 401 Unauthorized

当我尝试来自 Firefox 的请求时,它工作正常并且我得到了正确的响应,所以我知道服务器身份验证工作正常。

更新:将工作代码移至答案。

【问题讨论】:

标签: java rest httpclient http-status-code-401 digest-authentication


【解决方案1】:

以下代码对我有用

import java.util.Random;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.auth.DigestScheme;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;

/**
 * A simple example that uses HttpClient to execute an HTTP request against a
 * target site that requires user authentication.
 */
public class RestClient {

public static void main(String args[]) throws Exception {

    HttpHost targetHost = new HttpHost("localhost", 8001, "http");
    DefaultHttpClient httpclient = new DefaultHttpClient();

    final String userName = "admin";
    final String password = "password";

    httpclient.getCredentialsProvider().setCredentials(
            new AuthScope("localhost", 8001), 
            new UsernamePasswordCredentials(userName, password));   


    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate DIGEST scheme object, initialize it and add it to the local
    // auth cache
    DigestScheme digestAuth = new DigestScheme();
    // Suppose we already know the realm name
    digestAuth.overrideParamter("realm", "some realm");
    // Suppose we already know the expected nonce value
    digestAuth.overrideParamter("nonce", "whatever");
    authCache.put(targetHost, digestAuth);

    // Add AuthCache to the execution context
    BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

    HttpGet httpget = new HttpGet("http://localhost:8001/rest/test");

    try {
        HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
        }
        EntityUtils.consume(entity);
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 2011-02-26
    相关资源
    最近更新 更多