首页 文章

从Apache HTTPClient Basic Auth获取意外的401

提问于
浏览
1

我正在尝试使用example from the site与Apache HTTPClient 4.x进行基本身份验证,唯一的变化就是我没有得到我希望的结果 .

也就是说,随着日志记录调试,我得到:“DEBUG主客户端.DefaultHttpClient:1171 - 找不到凭据”,然后是来自服务器的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();

1 回答

  • 4

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

    new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT)
    

相关问题