Recently, when I was working on the Flutter project, I encountered an unsolved problem. When I used Image.network to load an image that required HTTPS and required a certificate verification, the console would send an error message indicating a certificate problem in English. I had been looking for a long time for the method of setting the certificate verification for the Flutter image loading, but there was no solution. The last not so good method is to directly change the Image.network load image method, see the source principle is the use of HttpClent, so then used a simpler way, in the load image initialization httpClent, directly ignore the SSL certificate authentication mode, It’s not a good idea, but it’s a temporary solution.

The Flutter native network requests HttpClent

Handle the verification certificate method in a simple and crude manner

_httpClient.badCertificateCallback = (X509Certificate cert,String host,int port){
  return true;
};
Copy the code

Flutter is the popular HTTP open source plugin for web requests

import 'package:http/http.dart'as http; // A simple and crude way to handle the certificate verification method http.clientsslClient() { var ioClient = new HttpClient() .. badCertificateCallback = (X509Certificate cert, String host, int port) =>true;
  http.Client _client = IOClient(ioClient);

  return_client; SslClient ().get(url) //post sslClient().post(url)Copy the code

The popular open source plugin for Flutter is Dio

(_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) { client.badCertificateCallback=(X509Certificate cert, String host, int port){ //if(cert.pem==PEM){ // Verify the certificate
//            return true;
//          }
          return true;
        };
      };

Copy the code

The last

Hopefully this will be useful at some point, but there is a risk of losing HTTPS security, so let’s just add a certificate verification method.

Support my words can pay attention to my public number, learn Android, small program, cross-platform development ~