简体   繁体   中英

HTTP-Response in Flutter

I have a problem with flutter. I want to get the HTTP-Response from a website, but it doesn´t work. The example works with other websites, but not with the required website. Code:

Future initiate() async {
var client = Client();
Response response = await client.get(
‘https://www.phwt.de’
);

I get this error:

E/flutter (18017): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: HandshakeException: Handshake error in client (OS Error:
E/flutter (18017): CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(handshake.cc:352))

The problem happens because flutter does not know who signed or issued the certificate for www.phwt.de . The certificate seems to have been signed by SwissSign AG, to make it work you have these options:

  1. Install the issuer's appropriate certificate ( SwissSign ) system-wide (this is OS specific).
  2. Either add the above certificates or www.phwt.de 's certificate to flutter/dart list's of trusted certificates (more info here and here ):
SecurityContext clientContext = new SecurityContext()
    ..setTrustedCertificates(file: 'my_trusted_certificates.pem');
var client = new HttpClient(context: clientContext);
var request = await client.getUrl(Uri.parse("https://www.phwt.de"));
var response = await request.close();
  1. Trust the certificate in the code itself by setting a callback to client.badCertificateCallback and checking if the signature matches (see here )
  2. Same as 3 but you don't check anything and just return true , effectively making any certificate in the world valid (this is potentially dangerous).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM