CERTIFICATE_VERIFY_FAILED:证书链中的自签名证书(handshake.cc:354))

时间:2020-11-02 17:24:40

标签: flutter dart https

我正在尝试使用flutter中的http程序包向openweathermap API发出GET请求, 这是我的代码

void getData() async{
    http.Response response = await http.get('http://samples.openweathermap.org/data/2.5/weather?lat=36&lon=140&appid={API%20key}');
    print(response.statusCode);
 }

Widget build(BuildContext context) {
    getData();
    return Scaffold(
    );
 }

但是当我在Android设备上运行该异常时,就会遇到此异常。

未处理的异常:HandshakeException:客户端中的握手错误(操作系统错误: CERTIFICATE_VERIFY_FAILED:证书链中的自签名证书(handshake.cc:354))

我已经检查了POSTMAN中的URI,它工作正常。 我尝试了可用的解决方案,但对我没有任何帮助。我有什么想念吗?

2 个答案:

答案 0 :(得分:0)

一旦我使用http而不是https,它就会起作用!?

答案 1 :(得分:0)

解决所有 http 请求 ssl 认证问题的最佳方法

它适用于两个平台(android 和 ios)

 class MyHttpOverrides extends HttpOverrides{
  @override
  HttpClient createHttpClient(SecurityContext context){
    return super.createHttpClient(context)
      ..badCertificateCallback = (X509Certificate cert, String host, int port)=> true;
  }
}

void main(){
    HttpOverrides.global = new MyHttpOverrides();
    runApp(new MyApp());
}