how to solve flutter CERTIFICATE_VERIFY_FAILED error while performing a POST request?

前端 未结 8 1326
孤街浪徒
孤街浪徒 2020-11-27 19:54

I am sending a post request in Dart. It is giving a response when I test it on API testing tools such as Postman. But when I run the app. It gives me the following error:- <

相关标签:
8条回答
  • 2020-11-27 20:32

    If you are using Dio library, just do this:

    Dio dio = new Dio();
    (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
        (HttpClient client) {
      client.badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;
      return client;
    };
    
    0 讨论(0)
  • 2020-11-27 20:42

    The best approach (I think) is to allow certificate for trusted hosts, so if your API host is "api.my_app" you can allow certificates from this host only:

    HttpClient client = new HttpClient();
    client.badCertificateCallback = ((X509Certificate cert, String host, int port) {
     final isValidHost = host == "api.my_app";
     return isValidHost;
    });
    

    If you have more hosts you can just add a new check there.

    0 讨论(0)
  • 2020-11-27 20:44

    The correct(but a bad) way to do it ,as I found out, is to allow all certificates.

    HttpClient client = new HttpClient();
    client.badCertificateCallback = ((X509Certificate cert, String host, int port) => true);
    
    String url ='xyz@xyz.com';
    
    Map map = { 
         "email" : "email" , 
         "password" : "password"
    };
    
    HttpClientRequest request = await client.postUrl(Uri.parse(url));
    
    request.headers.set('content-type', 'application/json');
    
    request.add(utf8.encode(json.encode(map)));
    
    HttpClientResponse response = await request.close();
    
    String reply = await response.transform(utf8.decoder).join();
    
    print(reply);
    
    0 讨论(0)
  • 2020-11-27 20:47
    import 'package:http/io_client.dart';
    import 'dart:io';
    import 'package:http/http.dart';
    import 'dart:async';
    import 'dart:convert';
    
        Future getAccessToken(String url) async {
          try {
            final ioc = new HttpClient();
            ioc.badCertificateCallback =
                (X509Certificate cert, String host, int port) => true;
            final http = new IOClient(ioc);
            http.post('url', body: {"email": "xyz@xyz.com", "password": "1234"}).then(
                (response) {
              print("Reponse status : ${response.statusCode}");
              print("Response body : ${response.body}");
              var myresponse = jsonDecode(response.body);
              String token = myresponse["token"];
            });
          } catch (e) {
            print(e.toString());
          }
        }
    
    0 讨论(0)
  • 2020-11-27 20:49

    Normally this problem occur when you unable to use external packages and it happens when there are strict firewall settings in your PC. Most of the time when you are using PC from company where they install special programs to block these services. So, disable them and hopefully it will work.

    0 讨论(0)
  • 2020-11-27 20:50

    Use tls 1.3 request URL, no problem. Example

    import 'dart:io';
    
    main() async {
      HttpClient client = new HttpClient();
      // tls 1.2 error
    //  var request = await client.getUrl(Uri.parse('https://shop.io.mi-img.com/app/shop/img?id=shop_88f929c5731967cbc8339cfae1f5f0ec.jpeg')); 
      // tls 1.3 normal
      var request = await client.getUrl(Uri.parse('https://ae01.alicdn.com/kf/Ud7cd28ffdf6e475c8dc382380d5d1976o.jpg'));
      var response = await request.close();
      print(response.headers);
      client.close(force: true);
    }
    
    0 讨论(0)
提交回复
热议问题