[英]How to Bypass SSL Certificate Verification in flutter?
How to Bypass SSL Certificate Verification in flutter?如何绕过 SSL 证书验证? Error: Handshake Exception: Handshake error in client(OS Error:CERTIFICATE_VERIFY_FAILED:self signed certificate(handshake.cc:345)
错误:握手异常:客户端中的握手错误(操作系统错误:CERTIFICATE_VERIFY_FAILED:自签名证书(handshake.cc:345)
You need to configure your HttpService to work with Self-Signed SSL local servers.您需要配置 HttpService 以使用自签名 SSL 本地服务器。 Like this:
像这样:
import 'dart:io';
import 'dart:convert';
class HttpService {
Future<dynamic> sendRequestToServer(dynamic model, String reqType, bool isTokenHeader, String token) async {
HttpClient client = new HttpClient();
client.badCertificateCallback =((X509Certificate cert, String host, int port) => true);
HttpClientRequest request = await client.postUrl(Uri.parse("https://${serverConstants.serverUrl}$reqType"));
request.headers.set('Content-Type', 'application/json');
if(isTokenHeader){
request.headers.set('Authorization', 'Bearer $token');
}
request.add(utf8.encode(jsonEncode(model)));
HttpClientResponse result = await request.close();
if(result.statusCode == 200) {
return jsonDecode(await result.transform(utf8.decoder)
.join());
} else {
return null;
}
}
}
It seems that you are using a self signed certificate, which is not trusted by the OS.您似乎正在使用操作系统不信任的自签名证书。 You can set it as trusted following these steps:
您可以按照以下步骤将其设置为受信任:
Create a class that overrides HttpOverrides
in the following way:通过以下方式创建一个覆盖
HttpOverrides
的类:
class MyHttpOverrides extends HttpOverrides {
@override
HttpClient createHttpClient(SecurityContext context) {
return super.createHttpClient(context)
..badCertificateCallback = (X509Certificate cert, String host, int port) {
//add your certificate verification logic here
return true;
};
}
}
Then, in your main method, instance your class and set it as the global HttpOverride:然后,在您的 main 方法中,实例化您的类并将其设置为全局 HttpOverride:
HttpOverrides.global = new DevHttpOverrides();
If badCertificateCallback
returns true
it will accept all bad certificates;如果
badCertificateCallback
返回true
,它将接受所有错误证书; if returns false it will reject a bad certificate.如果返回 false,它将拒绝错误的证书。
link.关联。 https://stackoverflow.com/a/66268556/11738366
https://stackoverflow.com/a/66268556/11738366
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.