CERTIFICATE_VERIFY_FAILED while using dio 3.0.10



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

[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: HandshakeException: Handshake error in client (OS Error: 
CERTIFICATE_VERIFY_FAILED: ok(handshake.cc:354))

我的代码(示例中提供(

Future login() async {
final request = {
"j_username": "user1",
"j_password": "pass1",
};
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);
//print(response.headers.toString());

}

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

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

(_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);

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

最新更新