Flutter 使用dio 3.0.10时证书验证失败

Flutter 使用dio 3.0.10时证书验证失败,flutter,Flutter,当我试图发布一个请求时,我得到 [VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: HandshakeException: Handshake error in client (OS Error: CERTIFICATE_VERIFY_FAILED: ok(handshake.cc:354)) 我的代码(示例中提供) }这意味着您的网站没有有效的证书 请添加以下代码以修复此问题 (_dio.httpClientAdapter as

当我试图发布一个请求时,我得到

[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: HandshakeException: Handshake error in client (OS Error: 
CERTIFICATE_VERIFY_FAILED: ok(handshake.cc:354))
我的代码(示例中提供)


}这意味着您的网站没有有效的证书

请添加以下代码以修复此问题

 (_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
         (HttpClient dioClient) {
       dioClient.badCertificateCallback =
           ((X509Certificate cert, String host, int port) => true);
       return dioClient;
     };
像这样

Future login() async {
final request = {
  "j_username": "user1",
  "j_password": "pass1",
};
(_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
             (HttpClient dioClient) {
           dioClient.badCertificateCallback =
               ((X509Certificate cert, String host, int port) => true);
           return dioClient;
         };

 final response = await _dio.post('/itim/restlogin/login.jsp', data: request);
//get cooking from response
final cookies = response.headers.map['set-cookie'];
if (cookies.isNotEmpty && cookies.length == 2) {
  final authToken = cookies[1].split(';')[0]; //it depends on how your server sending cookie
  //save this authToken in local storage, and pass in further api calls.

  aToken = authToken;
  print("authToken");
//saving this to global variable to refresh current api calls to add cookie.
  print(authToken);
}

print(cookies);

请确保您在拥有有效证书时对代码进行注释。

它与HttpClient一起工作,但我似乎无法用dio解决它,dio是否有解决它的选项?。或者只是使用一个有效的证书…是的,谢谢你,它就像一个符咒
Future login() async {
final request = {
  "j_username": "user1",
  "j_password": "pass1",
};
(_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
             (HttpClient dioClient) {
           dioClient.badCertificateCallback =
               ((X509Certificate cert, String host, int port) => true);
           return dioClient;
         };

 final response = await _dio.post('/itim/restlogin/login.jsp', data: request);
//get cooking from response
final cookies = response.headers.map['set-cookie'];
if (cookies.isNotEmpty && cookies.length == 2) {
  final authToken = cookies[1].split(';')[0]; //it depends on how your server sending cookie
  //save this authToken in local storage, and pass in further api calls.

  aToken = authToken;
  print("authToken");
//saving this to global variable to refresh current api calls to add cookie.
  print(authToken);
}

print(cookies);