Flutter网站http请求badCertificateCallback

时间:2020-04-24 15:12:09

标签: http flutter flutter-web dio

我想知道是否可以将我指向具有http badCertificateCallback的Web flutter库。我尝试了DIO,但它给我一个错误并提出了一个问题,但我还没有收到他们的来信

DIO代码:

Dio dio = new Dio(options);
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(client) {
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
};

Error: Expected a value of type 'DefaultHttpClientAdapter', but got one of type 'BrowserHttpClientAdapter'

我也尝试过http,但是它没有不良的证书回调,我们可以使用它,但是它与网络不兼容

HttpClient httpClient = new HttpClient();
    httpClient.badCertificateCallback =
        ((X509Certificate cert, String host, int port) => true);
    IOClient ioClient = new IOClient(httpClient);
response = await ioClient.post(url, body: data, headers: headers);

任何评论都会让您更加感动。

预先感谢, 丹尼尔

2 个答案:

答案 0 :(得分:0)

你可以把你的这一部分变成这一部分?

 HttpClient client = new HttpClient();
  client.badCertificateCallback =((X509Certificate cert, String  host, int port) => true);

答案 1 :(得分:0)

我以这种方式将 badCertificateCallback 与 DIO 结合使用:

//import 'package:get/get.dart' hide Response hide FormData; //<-- if you use get package
import 'package:dio/dio.dart';

void main(){
  HttpOverrides.global = new MyHttpOverrides();
  runApp(MyApp());
}

class MyHttpOverrides extends HttpOverrides{
  @override
  HttpClient createHttpClient(SecurityContext context){
    return super.createHttpClient(context)
      ..badCertificateCallback = ((X509Certificate cert, String host, int port) {
        final isValidHost = ["192.168.1.67"].contains(host); // <-- allow only hosts in array
        return isValidHost;
      });
  }
}

// more example: https://github.com/flutterchina/dio/tree/master/example
void getHttp() async {
  Dio dio = new Dio();
  Response response;
  response = await dio.get("https://192.168.1.67");
  print(response.data);
}