认证具有的HttpClient 4.x的一个请求 [英] Authenticating a single request with httpclient 4.x

查看:190
本文介绍了认证具有的HttpClient 4.x的一个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是由多个线程共享一个的HttpClient 实例。我想用它做一个身份验证的请求。由于只有单个的请求,应进行身份验证,我不想作为<一个描述修改的HttpClient 实例href=\"http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientAuthentication.java\"相对=nofollow>文档。以下是我已经计算出,而不是,这是行不通的。从我可以告诉,它看起来并不像 CredentialsProvider 正在使用的。任何提示?

  HttpContext的背景下= NULL;
如果(feedSpec.isAuthenticated()){
  上下文=新BasicHttpContext();
  CredentialsProvider credsProvider =新BasicCredentialsProvider();
  credsProvider.setCredentials(AuthScope.ANY,新UsernamePasswordCredentials(feedSpec.getHttpUsername(),feedSpec.getHttpPassword()));
  context.setAttribute(ClientContext.CREDS_PROVIDER,credsProvider);
  context.setAttribute(ClientPNames.HANDLE_AUTHENTICATION,真);
}
HTTPGET HTTPGET =新HTTPGET(feedSpec.getUri());
HTT presponse HTT presponse = httpClient.execute(HTTPGET,背景);


解决方案

原来我是连接到只提供NTLM身份验证服务器。我使用指南 rel=\"nofollow\">实施NTLM身份验证。我修改我的问题列出的code看起来像这样和它的作品:

  HttpContext的背景下= NULL;
如果(feedSpec.isAuthenticated()){
    上下文=新BasicHttpContext();
    CredentialsProvider credsProvider =新BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY,新NTCredentials(feedSpec.getHttpUsername(),feedSpec.getHttpPassword(),,));
    context.setAttribute(ClientContext.CREDS_PROVIDER,credsProvider);
}
HTTPGET HTTPGET =新HTTPGET(feedSpec.getUri());
HTT presponse HTT presponse = httpClient.execute(HTTPGET,背景);

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

解决方案

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

这篇关于认证具有的HttpClient 4.x的一个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆