Accepting self-signed certificate using badCertificateCallback - http-package

时间:2019-03-19 14:39:30

标签: dart dart-http

I want to accept the self-signed certificates which are not trusted. I'm unable to find a way to do this using http-package.

Can someone please help me modify below code to accept a self-signed certificate

import 'package:http/http.dart' show Client, Response;
import 'dart:convert';

  final Client _client = Client();

  Future<dynamic> postRequest(User user, String uri, String postBody,
      {decode: true}) async {
    try {
      var url = user.protocol + '://' + user.hostname + ':' + user.port + uri;
      String basicAuth =
          'Basic ' + base64Encode(utf8.encode('${user.username}:${user.password}'));
      var appHeaders = {
        'authorization': basicAuth,
        "Content-Type": "application/xml",
        "Accept": "application/json"
      };
      String resBody = await _client
          .post(url, body: postBody, headers: appHeaders)
          .then((Response res) => res.body);

      if (decode) {
        return json.decode(resBody);
      } else {
        return resBody;
      }
    } catch (e) {
      return e;
    }
  }

0 个答案:

没有答案