HTTP 1.1 doesn't have any mechanism to ask the server to cancel a request. It is possible to get the input/output/error stream from HttpURLConnection
and close them. That would throw IOException
on the client side allowing the client to clean up after cancel. The "persistent" or "keepAlive" TCP connection of HTTP 1.1 will continue to exist for future requests. Therefore, the server doesn't get to know about these events on the client side until it tries to write and fails.
HTTP 2 provides a separate frame called RST_STREAM
to close a stream or cancel a fired request. Since the Java 9 HttpClient
allows us to make asynchronous HTTP request returning a CompletableFuture
, there is now a more elegant way to cancel an ongoing request using the cancel()
method of CompletableFuture
.
My questions:
RST_STREAM
to the server when cancel()
is invoked?RST_STREAM
? Or any plan for future releases?In Java 11 to cancel an HTTP/2 request with the java.net.HttpClient
you would have to provide your own BodySubscriber
(or wrap one of the provided built-in) and cancel the Flow.Subscription
. This should reset the HTTP/2 stream.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With