使用 httpclient 4.x 验证单个请求

发布于 2024-08-27 02:11:53 字数 958 浏览 7 评论 0原文

我有一个由多个线程共享的 HttpClient 实例。我想用它来发出单个经过身份验证的请求。因为只应验证单个请求,所以我不想按照 文档。这是我已经解决的问题,但不起作用。据我所知,CredentialsProvider 看起来根本没有被使用。有什么建议吗?

HttpContext context = null;
if(feedSpec.isAuthenticated()) {
  context = new BasicHttpContext();
  CredentialsProvider credsProvider = new BasicCredentialsProvider();
  credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(feedSpec.getHttpUsername(), feedSpec.getHttpPassword()));
  context.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider);
  context.setAttribute(ClientPNames.HANDLE_AUTHENTICATION, true);
}
HttpGet httpGet = new HttpGet(feedSpec.getUri());
HttpResponse httpResponse = httpClient.execute(httpGet, context);

I have an HttpClient instance that's shared by a number of threads. I would like to use it to make a single authenticated request. Because only the single request should be authenticated, I don't want to modify the HttpClient instance as described in the documentation. Here's what I've worked out instead, which isn't working. From what I can tell, it doesn't look like the CredentialsProvider is being used at all. Any tips?

HttpContext context = null;
if(feedSpec.isAuthenticated()) {
  context = new BasicHttpContext();
  CredentialsProvider credsProvider = new BasicCredentialsProvider();
  credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(feedSpec.getHttpUsername(), feedSpec.getHttpPassword()));
  context.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider);
  context.setAttribute(ClientPNames.HANDLE_AUTHENTICATION, true);
}
HttpGet httpGet = new HttpGet(feedSpec.getUri());
HttpResponse httpResponse = httpClient.execute(httpGet, context);

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

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

发布评论

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

评论(1

冰雪梦之恋 2024-09-03 02:11:53

事实证明我连接的服务器只提供 NTLM 身份验证。我使用此处指南实现了 NTLM 身份验证。我修改了问题中列出的代码,使其看起来像这样并且有效:

HttpContext context = null;
if(feedSpec.isAuthenticated()) {
    context = new BasicHttpContext();
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(feedSpec.getHttpUsername(), feedSpec.getHttpPassword(), "", ""));
    context.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider);
}
HttpGet httpGet = new HttpGet(feedSpec.getUri());
HttpResponse httpResponse = httpClient.execute(httpGet, context);

It turns out the server I was connecting to was only offering NTLM authentication. I implemented NTLM authentication using the guide here. I modified the code listed in my question to look like so and it works:

HttpContext context = null;
if(feedSpec.isAuthenticated()) {
    context = new BasicHttpContext();
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(feedSpec.getHttpUsername(), feedSpec.getHttpPassword(), "", ""));
    context.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider);
}
HttpGet httpGet = new HttpGet(feedSpec.getUri());
HttpResponse httpResponse = httpClient.execute(httpGet, context);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文