我正在寻找可以连接到基于HTTP / 2的服务器的java客户端..服务器已经支持HTTP / 2 API。我还没有看到仍然支持HTTP / 2的最流行的Apache Http客户端https://hc.apache.org/。
Apache是否已经为支持Http / 2的Java客户端实现了一些实现?
如果没有,是否有一些Java客户端支持连接到HTTP / 2,最好是在Java 7上?
答案 0 :(得分:6)
Jetty提供了两个HTTP / 2 Java客户端API。两者都需要Java 8和强制使用ALPN,如here所述。
这些API基于HTTP2Client,它基于会话和流的HTTP / 2概念,并使用侦听器进行通知从服务器到达的HTTP / 2 帧。
if ($().selectpicker)
{
$('.selectpicker').selectpicker();
}
Jetty的 // Setup and start the HTTP2Client.
HTTP2Client client = new HTTP2Client();
SslContextFactory sslContextFactory = new SslContextFactory();
client.addBean(sslContextFactory);
client.start();
// Connect to the remote host to obtains a Session.
FuturePromise<Session> sessionPromise = new FuturePromise<>();
client.connect(sslContextFactory, new InetSocketAddress(host, port), new ServerSessionListener.Adapter(), sessionPromise);
Session session = sessionPromise.get(5, TimeUnit.SECONDS);
// Use the session to make requests.
HttpFields requestFields = new HttpFields();
requestFields.put("User-Agent", client.getClass().getName() + "/" + Jetty.VERSION);
MetaData.Request metaData = new MetaData.Request("GET", new HttpURI("https://webtide.com/"), HttpVersion.HTTP_2, requestFields);
HeadersFrame headersFrame = new HeadersFrame(metaData, null, true);
session.newStream(headersFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter()
{
@Override
public void onHeaders(Stream stream, HeadersFrame frame)
{
// Response headers.
System.err.println(frame);
}
@Override
public void onData(Stream stream, DataFrame frame, Callback callback)
{
// Response content.
System.err.println(frame);
callback.succeeded();
}
});
提供了一种使用different transports的方法,其中一个是HTTP/2 transport。应用程序将使用更高级别的HTTP API,但Jetty下面将使用HTTP / 2来传输HTTP语义。
通过这种方式,应用程序可以透明地使用HttpClient
提供的高级API,并分析在配置或启动代码中使用的传输。
HttpClient
答案 1 :(得分:2)
答案 2 :(得分:1)
从版本9.3开始,Jetty支持HTTP2。这包括服务器和the client。
答案 3 :(得分:0)
Apache httpclient-5 beta支持jdk9或更高版本的http / 2
示例:
public static void main(final String[] args) throws Exception {
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", 8082, "https");
final Future<AsyncClientEndpoint> leaseFuture = client.lease(target, null);
final AsyncClientEndpoint endpoint = leaseFuture.get(10, TimeUnit.SECONDS);
try {
String[] requestUris = new String[] {"/"};
CountDownLatch latch = new CountDownLatch(requestUris.length);
for (final String requestUri: requestUris) {
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);
}
引用:https://hc.apache.org/httpcomponents-client-5.0.x/examples-async.html