HTTP
HTTP: HyperText Transfer Protocol. A widely used version of the HTTP protocol, HTTP 1.1, was defined in June 1999, and the HTTP/2 standard was published in May 2015.
Differences between HTTP/2 and HTTP1.1
- HTTP/2 is in binary format rather than text format
- Full multiplexing
- Using header compression reduces overhead
- Let the server actively push data to the client
New Http Client in Java9
Official Feature:
motivation
The existing HttpURLConnection API and its implementation have numerous problems:
- The base URLConnection API was designed with multiple protocols in mind, nearly all of which are now defunct (ftp, gopher, etc.).
- The API predates HTTP/1.1 and is too abstract.
- It is hard to use, with many undocumented behaviors.
- It works in blocking mode only (i.e., one thread per request/response).
- It is very hard to maintain.
Abstract
Define a new HTTP client API that implements HTTP/2 and WebSocket, and can replace the legacy HttpURLConnection API. The API will be delivered as an incubator module, as defined in JEP 11, with JDK 9. This implies:
- The API and implementation will not be part of Java SE.
- The API will live under the jdk.incubtor namespace.
- The module will not resolve by default at compile or run time.
conclusion
Prior to Java9, HttpURLConnection was used, which had many problems, such as HTTP/1.1 only, difficult to use, blocking mode only, difficult to maintain, etc. In Java9, there is a new way to handle HTTP calls, a new HTTP Client that will replace HttpURLConnection, support for WebSocket and HTTP/2, and apis for streaming and server push.
The new HTTP Client is in the JDK.incubtor. httpClient module, and if you need to use it, you need to include it in the module declaration file (module-info.java) :
module main_module {
...
requiresjdk.incubtor.httpclient; . }Copy the code
Using an example
HttpClient client = HttpClient.newHttpClient();
HttpRequest req = HttpRequest.newBuilder(URI.create("http://hellomypastor.net" )).GET().build();
HttpResponse<String> response = client.send(req, HttpResponse.BodyHandler.asString());
System.out.println(response.statusCode());
System.out.println(response.version().name());
System.out.println(response.body());
}
Copy the code
Code on among