问题
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))
回答1:
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:
- Install the issuer's appropriate certificate (SwissSign) system-wide (this is OS specific).
- 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();
- Trust the certificate in the code itself by setting a callback to
client.badCertificateCallback
and checking if the signature matches (see here) - 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).
来源:https://stackoverflow.com/questions/58665747/http-response-in-flutter