Http2Client class

A pooled, multiplexed http.Client backed by HTTP/2 connections.

Every request is sent as its own HTTP/2 stream on a connection shared with other requests to the same host:port. Once a connection is carrying as many concurrent streams as it may - the lower of maxStreamsPerConnection and the server's own advertised limit - another connection is dialed rather than queuing behind the existing one. A connection dialed for one host is never reused for another.

This client speaks only HTTP/2, and does not fall back to HTTP/1.1: a server that does not negotiate h2 over ALPN is treated as an error rather than retried over HTTP/1.1.

onBadCertificate is forwarded as-is to SecureSocket.connect: returning true accepts a certificate that failed normal verification (expired, self-signed, wrong host, ...). It exists for tests and trusted private networks - do not use it to accept arbitrary certificates in production.

Annotations
  • @experimental

Constructors

Http2Client({int maxStreamsPerConnection = 100, int maxIdleConnections = 1, Duration settingsTimeout = const Duration(seconds: 10), int maxConcurrentHandshakes = 50, SecurityContext? context, bool onBadCertificate(X509Certificate certificate)?})

Properties

closed → Future<void>
Completes once close has finished shutting every connection down.
no setter
connectionCount → int
The number of connections currently pooled, across every host.
no setter
hashCode → int
The hash code for this object.
no setterinherited
maxIdleConnections → int
The maximum number of idle connections to keep per host.
final
maxStreamsPerConnection → int
The maximum number of concurrent HTTP/2 streams (i.e. requests) to multiplex onto a single connection before dialing another.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
settingsTimeout → Duration
How long to wait for a freshly dialed connection's peer to send its mandatory initial SETTINGS frame (RFC 7540 3.5).
final

Methods

close() → void
Rejects further requests, then waits for the in-flight ones to finish before closing every connection.
delete(Uri url, {Map<String, String>? headers, Object? body, Encoding? encoding}) → Future<Response>
Sends an HTTP DELETE request with the given headers to the given URL.
inherited
get(Uri url, {Map<String, String>? headers}) → Future<Response>
Sends an HTTP GET request with the given headers to the given URL.
inherited
Sends an HTTP HEAD request with the given headers to the given URL.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
patch(Uri url, {Map<String, String>? headers, Object? body, Encoding? encoding}) → Future<Response>
Sends an HTTP PATCH request with the given headers and body to the given URL.
inherited
post(Uri url, {Map<String, String>? headers, Object? body, Encoding? encoding}) → Future<Response>
Sends an HTTP POST request with the given headers and body to the given URL.
inherited
put(Uri url, {Map<String, String>? headers, Object? body, Encoding? encoding}) → Future<Response>
Sends an HTTP PUT request with the given headers and body to the given URL.
inherited
read(Uri url, {Map<String, String>? headers}) → Future<String>
Sends an HTTP GET request with the given headers to the given URL and returns a Future that completes to the body of the response as a String.
inherited
readBytes(Uri url, {Map<String, String>? headers}) → Future<Uint8List>
Sends an HTTP GET request with the given headers to the given URL and returns a Future that completes to the body of the response as a list of bytes.
inherited
send(BaseRequest request) → Future<StreamedResponse>
Sends an HTTP request and asynchronously returns the response.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited