本文介绍了使用OkHttp的HTTP/2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 OkHttp 客户端与HTTP/2服务器进行通信.

I am trying to communicate with a HTTP/2 server using OkHttp client.

已添加到Maven POM:

Added to Maven POM:

<dependency>
  <groupId>com.squareup.okhttp3</groupId>
  <artifactId>okhttp</artifactId>
  <version>3.2.0</version>
</dependency>    

这是我的测试代码:

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("https://http2.akamai.com/demo").build();
Response response = client.newCall(request).execute();
System.out.println("Protocol: " + response.protocol());
System.out.println(response.body().string());

但是当我运行它时,它会打印:

But when I run it it prints:

环境:Linux上的OpenJDK 8.

Environment: OpenJDK 8 on Linux.

您还需要其他东西吗?我看到了一个叫做"ALPN"的东西,但是对这个概念不太了解.

Do you need something additional? I saw something called "ALPN" but did not quite understand the concept.

推荐答案

HTTP/2需要ALPN,但直到JDK 9才在桌面Java中提供.称为 jetty-alpn 来启用它.

ALPN is required for HTTP/2, but it isn’t available in desktop Java until JDK 9. In Java 7 and Java 8 you’ll need a hack called jetty-alpn to enable it.

(对于Java 9,平台上有ALPN,但仅在即将发布的OkHttp 3.3中可用.)

(For Java 9 there’s ALPN on the platform but only in the upcoming OkHttp 3.3.)

这篇关于使用OkHttp的HTTP/2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 19:35