gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:04:09 24 4
gpt4 key购买 nike

我有一个由多个线程共享的 HttpClient 实例。我想用它来发出一个经过身份验证的请求。因为只应验证单个请求,所以我不想修改 HttpClient 实例,如 documentation 中所述.这是我已经解决的问题,但没有用。据我所知,它看起来根本没有使用 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);

最佳答案

原来我连接的服务器只提供 NTLM 身份验证。我使用指南 here 实现了 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);

关于java - 使用 httpclient 4.x 验证单个请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2516345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com