【问题标题】:Getting Unexpected 401 from Apache HTTPClient Basic Auth从 Apache HTTPClient Basic Auth 获取意外的 401
【发布时间】:2010-09-28 09:50:09
【问题描述】:

我正在尝试使用example from the site 对 Apache HTTPClient 4.x 进行基本身份验证,唯一的变化是我已将一些细节提取到常量中,但是我没有得到我希望的结果.

也就是说,在调试日志时,我得到:“DEBUG main client.DefaultHttpClient:1171 - Credentials not found”,然后是来自服务器的 401 错误。

我已手动验证我配置的凭据是否正确,并且“未找到凭据”消息让我相信凭据从未在请求​​中传递。

有什么想法我可能做错了吗?

DefaultHttpClient httpClient = new DefaultHttpClient();

    httpClient.getCredentialsProvider().setCredentials(
            new AuthScope(API_HOST, API_PORT),
            new UsernamePasswordCredentials(API_USERNAME, API_PASSWORD));

    HttpGet httpget = new HttpGet(API_TEST_URL);

    System.out.println("executing request" + httpget.getRequestLine());
    HttpResponse response = httpClient.execute(httpget);
    HttpEntity entity = response.getEntity();

    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());
    if (entity != null) {
        System.out.println("Response content length: " + entity.getContentLength());
    }
    if (entity != null) {
        entity.consumeContent();
    }

    httpClient.getConnectionManager().shutdown();

【问题讨论】:

    标签: java httpclient


    【解决方案1】:

    您确定 AuthScope 设置正确吗?试试这样设置看看有没有问题

    new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT)
    

    【讨论】:

    • Odd...您的更改按预期工作,因此它必须是 AuthScope,尽管它们似乎是正确的。哦,好吧,至少我现在走在了正确的轨道上。谢谢!
    猜你喜欢
    • 2018-07-23
    • 1970-01-01
    • 2022-08-03
    • 2012-12-23
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    相关资源
    最近更新 更多