apache-httpasyncclient - IllegalArgumentException:CloseableHttpAsyncClient 中的“身份验证方案可能不为空”
问题描述
我正在使用具有身份验证的代理运行一些异步 GET 请求。在执行 HTTPS 请求时,我总是在 2 个成功的异步请求后遇到异常:java.lang.IllegalArgumentException: Auth scheme may not be null
在没有代理的情况下执行 GET 请求或使用 http 而不是 https 时,从未发生异常。
来自Apache HttpAsyncClient 示例的示例
HttpHost proxy = new HttpHost("proxyname", 3128);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials("proxyuser", "proxypass"));
CloseableHttpAsyncClient httpClient = HttpAsyncClients.custom().setDefaultCredentialsProvider(credsProvider).build();
httpClient.start();
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
for (int i = 0; i < 3; i++) {
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(config);
httpClient.execute(httpGet, new FutureCallback<HttpResponse>() {
public void failed(Exception ex) {
ex.printStackTrace(); // Exception occures here afther 2nd iteration
}
public void completed(HttpResponse result) {
// works for the first and second iteration
}
public void cancelled() {
}
});
}
httpClient.close();
如果我用' http://httpbin.org/get '运行上面的代码,没有例外,但是如果我用' https://httpbin.org/get '运行它,我在2成功后得到以下异常要求:
java.lang.IllegalArgumentException: Auth scheme may not be null
at org.apache.http.util.Args.notNull(Args.java:54)
at org.apache.http.impl.client.AuthenticationStrategyImpl.authSucceeded(AuthenticationStrategyImpl.java:215)
at org.apache.http.impl.client.ProxyAuthenticationStrategy.authSucceeded(ProxyAuthenticationStrategy.java:44)
at org.apache.http.impl.auth.HttpAuthenticator.isAuthenticationRequested(HttpAuthenticator.java:88)
at org.apache.http.impl.nio.client.MainClientExec.needAuthentication(MainClientExec.java:629)
at org.apache.http.impl.nio.client.MainClientExec.handleResponse(MainClientExec.java:569)
at org.apache.http.impl.nio.client.MainClientExec.responseReceived(MainClientExec.java:309)
at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseReceived(DefaultClientExchangeHandlerImpl.java:151)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.responseReceived(HttpAsyncRequestExecutor.java:315)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:255)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:121)
at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591)
at java.lang.Thread.run(Thread.java:748)
注意:我使用的是 httpasyncclient 4.1.4
解决方案
如果这是您一直在执行的确切代码,那么问题就很明显了。欢迎来到偶数驱动编程的世界。
基本上会发生以下情况:
- 客户端通过在紧密循环中向客户端执行管道提交 3 个请求来发起 3 次消息交换
- 3 个消息交换排队等待执行
- 循环退出
- 客户端关闭已启动
- 现在客户端正在竞相执行 3 次发起的消息交换并同时关闭自己
- 如果幸运并且目标服务器足够快,则可能会在客户端关闭其 i/o 事件处理线程之前完成所有 3 次交换
- 如果运气不好或请求执行相对较慢,例如由于使用 TLS 传输安全性,一些消息交换可能会在处理过程中终止。这就是您在使用
https
scheme 而不是时看到失败的原因http
。
推荐阅读
- javascript - 在 HOC 中访问包装组件的表单元素?
- hosting - 如何在 Hostinger 上托管 Angular8 项目
- html - 如何将无单位的行高转换为百分比,以便获得正确的值?
- pandas - 仅当文件存在时才在 python pandas 中显示特定块的条件语句
- mysql - 当我将产品添加到 woocommerce 4.4.1 时出现错误消息
- spring - 配置 Spring 以在没有 Web 服务的情况下运行
- node.js - 从 atomicassets (wax.atomichub.io) 获取数据时,使用 discord.js 的 Node.js GET 错误
- c# - 使用 asParallel vs async function() 对集合进行迭代
- swiftui - SwiftUI App Lifecycle,为 GoogleSignIn 呈现 ViewController
- mongoose - 使用猫鼬崩溃在 NestJS 中实例化新文档