How to append to NetworkManager httpclient setting for skipping SSL sertificate check
Sometimes when we test network request on localhost and we don't have SSL sertificate, so we can not use Dio requests, because it shows status code 301, but on real site with SSL it works perfectly.
Here is default code:
NetworkManager({
required BaseOptions options,
bool? isEnableLogger,
InterceptorsWrapper? interceptor,
this.errorModel,
}) {
this.options = options;
_addLoggerInterceptor(isEnableLogger ?? false);
_addNetworkIntercaptors(interceptor);
//TODO: Http adapter has come
httpClientAdapter = DefaultHttpClientAdapter();
}
I need to add next code to settings for turning off SSL checking and prevent error:
(httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (HttpClient client) {
client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
return client;
};
I found next solution on DIO package and it works perfectly, but I don't know how to insert this code to upper code:
var _dio = Dio();
(_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (HttpClient client) {
client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
return client;
};
dio.send(...)
Please help me
Helo I can show your pr and problems right now. I'll check detail immediately
Thanks. The Custom Http Client Adapter is very helpful, especially this days, when on 30th September 2021, the root certificate that Let's Encrypt are currently using, the IdentTrust DST Root CA X3 certificate, expired.
Same error, could you please add the feature that @murat-ti mentioned? @VB10
@mfurkanyuceal i don't have required enviorment for instance SSL Pinnig. Https certificate verification There are two ways to verify the https certificate. Suppose the certificate format is PEM, the code like:
String PEM='XXXXX'; // certificate content (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) { client.badCertificateCallback=(X509Certificate cert, String host, int port){ if(cert.pem==PEM){ // Verify the certificate return true; } return false; }; }; Another way is creating a SecurityContext when create the HttpClient:
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) { SecurityContext sc = SecurityContext(); //file is the path of certificate sc.setTrustedCertificates(file); HttpClient httpClient = HttpClient(context: sc); return httpClient; }; In this way, the format of certificate must be PEM or PKCS12.
Maybe this can be help to you after if you can send a solution with pr, i'll so happy. thank u
@mfurkanyuceal https://pub.dev/packages/dio#https-certificate-verification
@mfurkanyuceal i don't have required enviorment for instance SSL Pinnig. Https certificate verification There are two ways to verify the https certificate. Suppose the certificate format is PEM, the code like:
String PEM='XXXXX'; // certificate content (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) { client.badCertificateCallback=(X509Certificate cert, String host, int port){ if(cert.pem==PEM){ // Verify the certificate return true; } return false; }; }; Another way is creating a SecurityContext when create the HttpClient:
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) { SecurityContext sc = SecurityContext(); //file is the path of certificate sc.setTrustedCertificates(file); HttpClient httpClient = HttpClient(context: sc); return httpClient; }; In this way, the format of certificate must be PEM or PKCS12.
Maybe this can be help to you after if you can send a solution with pr, i'll so happy. thank u
#31 pr