Java 9 中的 Http/2 Client 是什么?

javaobject oriented programmingprogramming更新于 2025/9/6 15:08:17

Java 9 中引入了 Http/2 Client API。它比 Http/1.1 具有更多性能改进,并且还支持 服务器端推送事件。这使网站 高效 并且 浏览速度更快。Http/2 Client 是一个名为 jdk.incubator.httpclient 的孵化器模块,这意味着所有功能仍未最终确定,并且 Java 的未来版本可能会出现新的变化。它导出包含所有公共 API 的 jdk.incubator.http 包。

要使用  Http/2 客户端,我们需要使用 incubator 模块,我们只需使用 "–add-modules 命令将 httpclient 模块  传递到 JShell 中,如下所示

C:\>jshell -v --add-modules jdk.incubator.httpclient
| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro

示例

jshell>导入 jdk.incubator.http.*;

jshell> HttpClient httpClient = HttpClient.newHttpClient();
httpClient ==> jdk.incubator.http.HttpClientImpl@534df152
| 创建变量 httpClient : HttpClient

jshell> HttpRequest httpRequest = HttpRequest.newBuilder().uri(new URI("https: //www.google.com")).GET().build();
httpRequest ==> https://www.google.com GET
| 创建变量 httpRequest : HttpRequest

jshell> HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandler.asString());
httpResponse ==> jdk.incubator.http.HttpResponseImpl@609cd4d8
| 创建变量 httpResponse : HttpResponse

jshell> System.out.println(httpResponse.statusCode());
403

jshell> System.out.println(httpResponse.body());
由 CentOS 提供支持的 Apache HTTP 服务器测试页面
正在测试 123..

此页面用于测试 Apache HTTP 服务器 安装后的正常运行。如果您可以阅读此页面,则表示此站点正常运行。此服务器由 CentOS 提供支持。

您刚刚访问的网站出现问题或正在进行例行维护。

如果您想让此网站的管理员知道您看到的是此页面而不是您预期的页面,您应该向他们发送电子邮件。一般来说,发送到名称"webmaster er"并指向网站域名的邮件应该会到达相应的人员。例如,如果您在访问 www.example.com 时遇到问题,您应该发送电子邮件至"webmaster@example .com"。 您是管理员吗?

您应该将网站内容添加到目录 /var/www/html/。

要防止此页面被使用,请按照文件 /etc/httpd/conf.d/welcome.conf 中的说明进行操作。

推广 Apache 和 CentOS

您可以自由地在 Apache 和 CentOS Linux 驱动的 HTTP 服务器上使用以下图像。感谢您使用 Apache 和 CentOS!


相关文章


有用资源