HTTP-Response in Flutter

我的梦境 提交于 2020-01-16 09:00:39

问题


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:

  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).


来源:https://stackoverflow.com/questions/58665747/http-response-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!