Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Apache or some other CLIENT JAVA implementation support HTTP/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.

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

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

like image 661
Neeraj Krishna Avatar asked Jan 16 '16 04:01

Neeraj Krishna


People also ask

How can I tell if http 2 is enabled?

Google Chrome offers a quick and easy way to check if HTTP/2 is supported on your SSL-enabled site. First, visit your site in Chrome over HTTPS. There you'll see your site listed with protocol h2, confirming your site works over HTTP/2.

What is Apache HTTP client?

Http client is a transfer library. It resides on the client side, sends and receives Http messages. It provides up to date, feature-rich, and an efficient implementation which meets the recent Http standards.

Which HTTP client is best in Java?

If we do not want to add any external libraries, Java's native HTTPClient is the first choice for Java 11+ applications. Spring WebClient is the preferred choice for Spring Boot applications more importantly if we are using reactive APIs.


1 Answers

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

Low level APIs

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

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.

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/");
like image 173
sbordet Avatar answered Oct 12 '22 01:10

sbordet