1

I want to get a Http-response from the Website 'https://www.phwt.de'. I tried to add the certificate in Flutter, but it doesn't work. The pem-file was added into the directory 'assets/Certificates' and it was added in pubspec.yaml. The error is, that the file cannot be opened.

Future initiate() async {
  SecurityContext clientContext = new SecurityContext()
    ..setTrustedCertificates('assets/Certificates/personal.pem');
  var client = new HttpClient(context: clientContext);
  var request = await client.getUrl(Uri.parse("https://www.phwt.de"));
  var response = await request.close();

  return response;
}

Error: OS Error: No such file or directory, errno = 2

2
  • Does this answer your question? HTTP-Response in Flutter Nov 6, 2019 at 14:58
  • See that comment on the answer of the linked question. This solution will not work unless that server has fixed its certificate chain. Nov 6, 2019 at 14:59

2 Answers 2

3

You can pass it as binary data:

  ByteData bytes = await rootBundle.load('assets/Certificates/personal.pem');
  SecurityContext clientContext = new SecurityContext()
    ..setTrustedCertificatesBytes(bytes.buffer.asUint8List());
0
0
  // Set SecurityContext to not trust the OS's certificates
   SecurityContext(withTrustedRoots: false);

  // Load certificate file
   ByteData data = await rootBundle.load('assets/cert/Certificate.pem');

   SecurityContext context = SecurityContext.defaultContext;
  // Trust the certificate
   context.setTrustedCertificatesBytes(data.buffer.asUint8List());

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.