9

I was wondering if any you can point me to a web flutter library that had http badCertificateCallback. I tried DIO but it is giving me an error and submit an issue but I haven't heard from them yet

DIO code:

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'

I also tried http, but it doesn't have a bad Certificate Callback, we could use this but it isn't web-compatible

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);

Any comment will be more that apreciate.

Thanks in advance, Daniel

Daniel Hernandez
  • 338
  • 3
  • 13

4 Answers4

1

you can just turn this part of your to this one

 HttpClient client = new HttpClient();
  client.badCertificateCallback =((X509Certificate cert, String  host, int port) => true);
Boris Kamtou
  • 454
  • 4
  • 5
  • 1
    this won't work on Fultter-Web `Unsupported operation: Platform._version` – Yonatan Mar 16 '22 at 14:07
  • I did it this way, (dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient = (HttpClient dioClient) { dioClient.badCertificateCallback = ((X509Certificate cert, String host, int port) => true); return dioClient; } as CreateHttpClient?; but dio.get() doesnt work. im not sure what the right syntax for this is. onHttpClientCreate worked for me – chitgoks Aug 04 '23 at 13:20
1

I use badCertificateCallback with DIO this way:

//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);
}
Zorro
  • 1,085
  • 12
  • 19
1

Make a http Client like this,

import 'dart:convert';
import 'dart:io';
import 'package:dio/adapter.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'dart:io' as IO;
....
....
....
/// CLIENT
static Future<Dio> _dioClient() async {

Dio dio = Dio(await _getOptions()); // Getting Headers and Other data

if(!kIsWeb){
  (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
      (IO.HttpClient client) {
    client.badCertificateCallback =
        (X509Certificate cert, String host, int port) => true;
    return client;
  };
}
return dio;                                                             
}
  • This one actually works. but onHttpClientCreate is now deprecated. do you have the updated code for .createHttpClient ? – chitgoks Aug 04 '23 at 13:22
0

With 5.2.0 release of dio, according to the documentation we can do the following.

dio.httpClientAdapter = IOHttpClientAdapter(
          createHttpClient: () {
            // Don't trust any certificate just because their root cert is trusted.
            final HttpClient client =
                HttpClient(context: SecurityContext(withTrustedRoots: false));
            // You can test the intermediate / root cert here. We just ignore it.
            client.badCertificateCallback =
                ((X509Certificate cert, String host, int port) => true);
            return client;
          },
);
Asim Khan
  • 508
  • 1
  • 7
  • 21