从 Apache HTTPClient 基本身份验证获取意外的 401

发布于 2024-09-25 07:34:11 字数 1298 浏览 3 评论 0原文

我正在尝试使用 Apache HTTPClient 4.x 使用 来自网站的示例,唯一的变化是我将一些详细信息提取到常量中,但是我没有得到结果是希望的。

也就是说,当日志记录打开进行调试时,我收到:“DEBUG main client.DefaultHttpClient:1171 - Credentials not find”,然后是来自服务器的 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();

I'm attempting to do basic auth with Apache HTTPClient 4.x using the example from the site, the only change being that I've extracted some details out into constants, however I'm not getting the results I was hoping for.

Namely, with the logging turned up to debug, I'm getting: "DEBUG main client.DefaultHttpClient:1171 - Credentials not found", followed by a 401 error from the server.

I've manually validated that the credentials I've configured are correct, and the "Credentials not found" message leads me to believe the credentials were never passed in the request.

Any thoughts on what I might be doing wrong?

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢欲 2024-10-02 07:34:11

您确定 AuthScope 设置正确吗?尝试这样设置看看是否存在问题

new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT)

Are you sure the AuthScope is set correctly? Try setting it like this just to see if the problem is there

new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文