IllegalArgumentException:CloseableHttpAsyncClient中的“身份验证方案不能为空”

时间:2019-03-29 09:19:59

标签: apache-httpasyncclient

我正在使用带有身份验证的代理来运行一些异步GET请求。在执行HTTPS请求时,在2个成功的异步请求之后,我总是遇到异常: java.lang.IllegalArgumentException:身份验证方案不能为空

在不使用代理执行GET请求或使用http而不是https的情况下,从未发生异常。

来自Apache HttpAsyncClient Examples

的示例
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

1 个答案:

答案 0 :(得分:1)

如果这是您一直在执行的确切代码,则问题很明显。欢迎来到偶驱动编程的世界。

基本上会发生以下情况:

  1. 客户端通过紧密地向客户端执行管道提交3个请求来发起3个消息交换
  2. 3条消息交换被排队 执行
  3. 循环退出
  4. 客户端关闭已启动
  5. 现在,客户端正在竞相执行3次发起的消息交换并同时关闭自身
  6. 如果幸运的是目标服务器足够快,那么目标服务器可能会在客户端关闭其I / O事件处理线程之前进行所有3次交换
  7. 如果不幸,或者由于使用TLS传输安全性(例如由于使用TLS传输安全性)而导致请求执行相对较慢时,某些消息交换可能会在过程中间终止。这就是使用https方案而不是http时看到失败的原因。