
Http/2 是Http 协议的较新版本。 Http/2 的改进包括关注数据在服务器和客户端之间的构建和传输方式。在这个新版本的Http/2协议中,为Http客户端、请求和响应定义了单独的类强>。新的 API 使 Http 连接更容易维护、更快速,并且无需第三方库即可实现响应速度更快的应用程序。
新的 API 通过三个类处理 HTTP 连接。
在下面的代码片段中,我们需要向特定的 URL 发送请求并接收响应。
<strong>// Create an HttpClient object </strong>
<strong> HttpClient </strong>httpClient = <strong>HttpClient.newHttpClient()</strong>;
System.out.println(<strong>httpClient.version()</strong>);
<strong>// Build a HTTPRequest
</strong><strong> HttpRequest </strong>httpRequest = HttpRequest.newBuilder().uri(new URI("https://www.tutorialspoint.com/")).<strong>GET</strong>().build(); <strong>// create a GET request for the given URI</strong>
<strong>Map</strong><strong><String, List<String></strong>> headers = httpRequest.headers().map();
headers.forEach((k, v) -> System.out.println(k + "-" + v));
<strong>// Send the request
</strong><strong> HttpResponse </strong>httpResponse = httpClient.<strong>send</strong>(httpRequest, HttpResponse.BodyHandler.asString());
<strong>// Output the body of the response
</strong> System.out.println("Response: " + httpResponse.<strong>body()</strong>);以上就是在Java 9中有哪些不同的Http/2客户端类?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号