gpt4 book ai didi

java - HTTP2 url 在浏览器中加载成功但在 httpclient5 中加载失败

转载 作者:行者123 更新时间:2023-11-28 23:16:20 28 4
gpt4 key购买 nike

我正在尝试创建一个 httpclient 5 示例,该示例使用带有此 example 的 asynchttpclient(异步多路复用)加载 URL ,我已经使用 conf/server.xml 配置配置了 tomcat 9 来接受 http2 协议(protocol),如下所示

<Connector
protocol="org.apache.coyote.http11.Http11NioProtocol"
port="8443" maxThreads="200"
scheme="https" secure="true" SSLEnabled="true"
keystoreFile="conf/xxx.keystore" keystorePass="xxx#"
clientAuth="false" sslProtocol="TLS"><UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol"/></Connector>

和我的程序如下,

    final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(new TrustAllStrategy()).build();
final PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create().setTlsStrategy(new H2TlsStrategy(sslContext, NoopHostnameVerifier.INSTANCE)).build();
final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
final MinimalHttpAsyncClient client = HttpAsyncClients.createMinimal(HttpVersionPolicy.FORCE_HTTP_2, H2Config.DEFAULT, null, ioReactorConfig, connectionManager);


client.start();

final HttpHost target = new HttpHost("localhost", 8084, "https");
System.out.println(target.getPort());
final Future<AsyncClientEndpoint> leaseFuture = client.lease(target, null);
final AsyncClientEndpoint endpoint = leaseFuture.get(45, TimeUnit.SECONDS);
try {
final String[] requestUris = new String[] {"/index.html"};

final CountDownLatch latch = new CountDownLatch(requestUris.length);
for (final String requestUri: requestUris) {
final SimpleHttpRequest request = SimpleHttpRequest.get(target, requestUri);

endpoint.execute(SimpleRequestProducer.create(request), SimpleResponseConsumer.create(), new FutureCallback<SimpleHttpResponse>() {
@Override
public void completed(final SimpleHttpResponse response) {
latch.countDown();
System.out.println(requestUri + "->" + response.getCode());
System.out.println(response.getBody());
}

@Override
public void failed(final Exception ex) {
latch.countDown();
System.out.println(requestUri + "->" + ex);
ex.printStackTrace();
}

@Override
public void cancelled() {
latch.countDown();
System.out.println(requestUri + " cancelled");
}

});
}
latch.await();
} catch (Exception e) {
e.printStackTrace();
}finally {
endpoint.releaseAndReuse();
}

client.shutdown(ShutdownType.GRACEFUL);

tomcat 配置在使用 h2 协议(protocol)在 borwser 中加载页面时工作,但它在 httpclient 5 中失败并出现以下异常

/index.html->org.apache.hc.core5.http.ConnectionClosedException: Connection closed
org.apache.hc.core5.http.ConnectionClosedException: Connection closed
at org.apache.hc.core5.http2.impl.nio.FrameInputBuffer.read(FrameInputBuffer.java:146)
at org.apache.hc.core5.http2.impl.nio.AbstractHttp2StreamMultiplexer.onInput(AbstractHttp2StreamMultiplexer.java:415)
at org.apache.hc.core5.http2.impl.nio.AbstractHttp2IOEventHandler.inputReady(AbstractHttp2IOEventHandler.java:63)
at org.apache.hc.core5.http2.impl.nio.ClientHttp2IOEventHandler.inputReady(ClientHttp2IOEventHandler.java:38)
at org.apache.hc.core5.reactor.InternalDataChannel.onIOEvent(InternalDataChannel.java:117)
at org.apache.hc.core5.reactor.InternalChannel.handleIOEvent(InternalChannel.java:50)
at org.apache.hc.core5.reactor.SingleCoreIOReactor.processEvents(SingleCoreIOReactor.java:173)
at org.apache.hc.core5.reactor.SingleCoreIOReactor.doExecute(SingleCoreIOReactor.java:123)
at org.apache.hc.core5.reactor.AbstractSingleCoreIOReactor.execute(AbstractSingleCoreIOReactor.java:80)
at org.apache.hc.core5.reactor.IOReactorWorker.run(IOReactorWorker.java:44)
at java.lang.Thread.run(Thread.java:748)

如有任何帮助,我们将不胜感激

提前致谢

在启用 tomcat 日志时,我可以看到以下内容,

org.apache.coyote.AbstractProtocol$ConnectionHandler.process Failed to create Processor for negotiated protocol [h2c]

当我将客户端代码更新到 java 9 时,即使这个问题也得到了解决,但是否有可能让它与 java 8 一起工作。

p.s:我知道 java 8 不支持 ALPN,但如果有任何方法可以使它与 java 8 一起工作,请告诉我。

最佳答案

通过使用 concrypt 作为默认提供程序并使用自定义 TLSStrategy,我们可以使其与 jdk1.8 一起使用,引用,

https://stackoverflow.com/a/53399363/2236001

关于java - HTTP2 url 在浏览器中加载成功但在 httpclient5 中加载失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48860796/

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