zeus
zeus

Reputation: 12965

How with NetHttpClient (or Indy) do an HTTP2 request?

I use netHttpClient to request several images. But as i know instead of doing several TCP requests to download each images, I can request all of them in one single TCP request. This is particularly important for HTTPS where each https TCP request must also do several other requests (HTTPS requires an initial handshake which can be very slow). As i know setting keep-alive will not avoid to requires this initial handshake but i m not sure (someone can confirm it?)

How to do http2 request in NetHttpClient / Indy ?

Upvotes: 0

Views: 1939

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 598029

But as i know instead of doing several TCP requests to download each images, I can request all of them in one single TCP request.

Whether you use HTTP 0.9, 1.x, or 2, you have to send a separate HTTP request for each image. HTTP 1.1 and later allows you to keep the TCP connection open after a response, so you can send multiple HTTP requests over a single TCP connection (even pipeline them for better network bandwidth usage). But you still have to send a separate HTTP request for each image whether you use 1 or more TCP connections.

If you want to retrieve multiple images in parallel, HTTP 2 allows that over a single TCP connection by allowing requests and responses to be multiplexed. With HTTP 1.1 and earlier, only 1 HTTP request/response pair can be processed at a time, so you would need to use multiple TCP connections for parallel processing.

This is particularly important for HTTPS where each https TCP request must also do several other requests (HTTPS requires an initial handshake which can be very slow).

That is exactly why HTTP 1.1 introduced the keep-alive mechanism in the first place, and made it the default behavior. Establishing a new TCP connection for each HTTP request is time consuming, even more so when SSL/TLS is involved for HTTPS. So, using a keep-alive will allow you to use only 1 TCP connection and thus require only 1 SSL/TLS handshake.

As i know setting keep-alive will not avoid to requires this initial handshake

No, it will not eliminate the handshake, but it will minimize the number of handshakes that you need to perform.

How to do http2 request in NetHttpClient / Indy ?

At this time, neither one of the them supports HTTP 2 (it is a TODO item for Indy, though), and server-side support for HTTP 2 is not widespread, either. But, you don't actually need HTTP 2, HTTP 1.1 with keep-alive will suffice.

Upvotes: 3

Arnaud Bouchez
Arnaud Bouchez

Reputation: 43053

HTTP/2 is not supported by Indy yet.

HTTP/2 is a much more complex protocol than HTTP/1 so implementing it demands a lot of work and testing.

Anyway, I guess you don't need HTTP/2 in your case. Setting HTTP/1 keep-alive will let your connection remain available after a single TLS handshake, and you will be able to download all images with your full bandwidth. For very small images, HTTP/2 requests interleaving may make it faster, but for images big enough, it will be fast.

If you expect performance, either create several clients, or prepare all images on the server side as a single blob (e.g. stored in a zip with no compression) and download it at once.

Upvotes: 2

Related Questions