5

我正在使用具有身份验证的代理运行一些异步 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

4

1 回答 1

1

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

基本上会发生以下情况:

  1. 客户端通过在紧密循环中向客户端执行管道提交 3 个请求来发起 3 次消息交换
  2. 3 个消息交换排队等待执行
  3. 循环退出
  4. 客户端关闭已启动
  5. 现在客户端正在竞相执行 3 次发起的消息交换并同时关闭自己
  6. 如果幸运并且目标服务器足够快,则可能会在客户端关闭其 i/o 事件处理线程之前完成所有 3 次交换
  7. 如果运气不好或请求执行相对较慢,例如由于使用 TLS 传输安全性,一些消息交换可能会在处理过程中终止。这就是您在使用httpsscheme 而不是时看到失败的原因http
于 2020-05-15T18:11:06.547 回答