Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter web http request badCertificateCallback

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

like image 676
Daniel Hernandez Avatar asked Apr 24 '20 15:04

Daniel Hernandez


3 Answers

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;                                                             
}
like image 95
Shitab Mushfiq-ul Islam Avatar answered Oct 19 '22 07:10

Shitab Mushfiq-ul Islam


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);
}
like image 44
Zorro Avatar answered Oct 19 '22 07:10

Zorro


you can just turn this part of your to this one 👇

 HttpClient client = new HttpClient();
  client.badCertificateCallback =((X509Certificate cert, String  host, int port) => true);
like image 1
Boris Kamtou Avatar answered Oct 19 '22 08:10

Boris Kamtou