dio_http2_adapter 0.0.1 copy "dio_http2_adapter: ^0.0.1" to clipboard
dio_http2_adapter: ^0.0.1 copied to clipboard

outdated

A Dio HttpAdapter which support Http/2.0.

Http2Adapter Pub #

A Dio HttpClientAdapter which implements Http/2.0 .

Getting Started #

Install #

dependencies:
  dio_http2_adapter: x.x.x  #latest version

Usage #

import 'package:dio/dio.dart';
import 'package:dio_http2_adapter/http2.dart'; 

main() async {
  var dio = Dio()
    ..options.baseUrl = "https://www.ustc.edu.cn/"
    ..httpClientAdapter = Http2Adapter();

  Response<String> response;
  response = await dio.get("/?xx=6");
  print(response.data);
  response = await dio.get("2062/list.htm");
  print(response.data);
}

ConnectionManager #

ConnectionManager is used to manager the connections that should be reusable. The main responsibility of ConnectionManager is to implement a connection reuse strategy for http2.

dio.httpClientAdapter = Http2Adapter(
  ConnectionManager(
    idleTimeout: 10000,
    /// Ignore bad certificate
    onClientCreate: (_, clientSetting) => clientSetting.onBadCertificate = (_) => true,
  ),
);
  • idleTimeout: Sets the idle timeout(milliseconds) of non-active persistent connections. For the sake of socket reuse feature with http/2, the value should not be less than 1000 (1s).
  • onClientCreate:Callback when socket created. We can set trusted certificates and handler for unverifiable certificates.

You can also custom a connection manager with a specific connection reuse strategy by implementing the ConnectionManager.

Attention #

Http2Adapter doesn't support redirection yet, so we can't get information about redirection through Response, for example, the following code will be true

response.redirects==null 
response.isRedirect==null;  

However, we can use the status code to determine whether redirection is necessary:

if(response.statusCode==301||response.statusCode==302){
  String redirectUrl=response.headers.value("location");
  // do something
}