Apache或其他一些CLIENT JAVA实现是否支持HTTP / 2? [英] Does Apache or some other CLIENT JAVA implementation support HTTP/2?

查看:172
本文介绍了Apache或其他一些CLIENT JAVA实现是否支持HTTP / 2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找可以连接到基于HTTP / 2的服务器的java客户端..服务器已经支持HTTP / 2 API。我没有看到仍然支持HTTP的最流行的Apache Http客户端 https://hc.apache.org/ / 2。

I'm looking for java client that can connect to a HTTP/2 based server.. The server is already supporting HTTP/2 API. I don't see the most popular Apache Http client https://hc.apache.org/ still supporting HTTP/2.

Apache是​​否已经为支持Http / 2的Java客户端实现了一些实现?

Does Apache have some implementation for Java client already that supports Http/2?

如果没有,是否有一些支持连接到HTTP / 2的Java客户端最好是在Java 7上?

If not, Is there some java client that supports connecting to HTTP/2 preferably on Java 7?

推荐答案

Jetty 提供了两个HTTP / 2 Java客户端API。两者都需要Java 8和强制使用ALPN,如 here

Jetty's provides two HTTP/2 Java client APIs. Both require Java 8 and the mandatory use of the ALPN, as explained here.

这些API基于 HTTP2Client ,它基于HTTP / 2概念 session streams 并使用侦听器通知从服务器到达的HTTP / 2

These APIs are based on HTTP2Client, it's based on the HTTP/2 concepts of session and streams and uses listeners to be notified of the HTTP/2 frames that arrive from the server.

    // 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();
        }
    });



高级API



Jetty的 HttpClient 提供了一种使用不同的传输,其中之一是 HTTP / 2传输。应用程序将使用更高级别的HTTP API,但Jetty下面将使用HTTP / 2来传输HTTP语义。

High Level APIs

Jetty's HttpClient provides a way to use different transports, one of which is the HTTP/2 transport. Applications will use the higher level HTTP APIs, but underneath Jetty will use HTTP/2 to transport the HTTP semantic.

这样,应用程序可以使用提供的高级API通过 HttpClient 透明地,并分析在配置或启动代码中使用的传输。

In this way, applications can use the high level APIs provided by HttpClient transparently, and factor out what transport to use in configuration or startup code.

    // Setup and start HttpClient with HTTP/2 transport.
    HTTP2Client http2Client = new HTTP2Client();
    SslContextFactory sslContextFactory = new SslContextFactory();
    HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP2(http2Client), sslContextFactory);
    httpClient.start();

    // Make a request.
    ContentResponse response = httpClient.GET("https://webtide.com/");

这篇关于Apache或其他一些CLIENT JAVA实现是否支持HTTP / 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆