Comments (19)
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (HttpClient client) {
client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
return client;
};
from dio.
it doesn't seem like onHttpClientCreate() is ever reached:
Dio dio = new Dio();
dio.onHttpClientCreate = (HttpClient client) {
print('onHttpClientCreate entered...'); // this code is never reached
client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
return client;
};
Response<String> response=await dio.get(url); //CERTIFICATE_VERIFY_FAILED:ok
from dio.
How to disable SSL certificate check using Dio in WEB ENVIRONMENT?
from dio.
Closing this issue since a solution was found.
class MyHttpOverrides extends HttpOverrides{
@OverRide
HttpClient createHttpClient(SecurityContext context){
HttpClient client= super.createHttpClient(context); //<<--- notice 'super'
client.badCertificateCallback = (X509Certificate cert, String host, int port)=> true;
return client;
}
}
void main(){
HttpOverrides.global = new MyHttpOverrides();
runApp(new App());
}
from dio.
Actual on March 2024:
callback is deprecated. Now you should use onHttpClientCreate
createHttpClient
and return your own HttpClient
instance:
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:dio/io.dart';
Dio createDio({required String baseUrl, bool trustSelfSigned = false}) {
// initialize dio
final dio = Dio()
..options.baseUrl = baseUrl;
// allow self-signed certificate
(dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () {
final client = HttpClient();
client.badCertificateCallback = (cert, host, port) => trustSelfSigned;
return client;
};
return dio;
}
from dio.
@pepie Ok, May you overwrite OnHttpClientCreate
callback somewhere ?
from dio.
You should create a SecurityContext
object to trust your certificate in onHttpClientCreate
callback, for examples:
dio.onHttpClientCreate = (HttpClient client) {
// create a `SecurityContext` instance to trust you certificate
return HttpClient(securityContext)
};
More about SecurityContext
please refer to dart doc -SecurityContext
from dio.
Consider adding support for badCertificateCallback.
HttpClient httpClient = new HttpClient()
..badCertificateCallback =
((X509Certificate cert, String host, int port) => trustSelfSigned);
perhaps as an option to the constructor:
new Dio({acceptSelfSignedCert: false})
from dio.
Are you sure? I have run the code as follows:
dio.onHttpClientCreate = (HttpClient client) {
print("dio xxxx");
client.badCertificateCallback =
(X509Certificate cert, String host, int port) {
return true;
};
};
Console log
I/flutter (18963): dio xxxx
Which version of dio do you use? 0.1.3?
from dio.
Yes, It never ran on my box.
It's a little weird.
I've submitted a pull request to support self-signed certs.
#33
Please consider. This works.
Another option would be to allow one to pass the entire callback, instead of setting trustSelfSignedCerts to true.
Great plugin btw!!
from dio.
@wendux I created a new project and was able to confirm dio.onHttpClientCreate() ran successfully.
I'll have to figure out why it wasn't triggered in the first project, bu the problem seems to be local.
Using version 0.1.3
FYI
from dio.
I'm guessing that's the case.
I'll send an update if I discover anything strange.
Thanks!
from dio.
what about this issuse ?
from dio.
It seems that @pepie found a solution (check #33 ). Should this issue be closed? Does this specific case need to be documented?
from dio.
Dio dio = new Dio();
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(HttpClient client) {
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
return client;
};
from dio.
DioForNative dio = DioForNative();
DefaultHttpClientAdapter httpClient = dio.httpClientAdapter;
httpClient.onHttpClientCreate = (HttpClient client) {
client.badCertificateCallback =
(X509Certificate cert, String host, int port) {
return true;
};
};
and then you can make dio.post and dio.get requests.
from dio.
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (HttpClient client) { client.badCertificateCallback = (X509Certificate cert, String host, int port) => true; return client; };
This is not SAFE for production versions!
We need other reply to resolve it, anyone?
from dio.
@bambinoua Have you found a solution for flutter web?
from dio.
In my case I needed to use an self signed certificate and the solution was this:
Future<Response> secureRequest(
String path, {
method = 'GET',
Map<String, dynamic>? queryParameters,
dynamic data,
Map<String, String>? headers,
}) async {
// convert my certificate to pem
// openssl x509 -in client.crt -out client.pem
// openssl rsa -in client.key -out clientkey.pem
ByteData dataCRT = await rootBundle.load('assets/ca/client.pem');
ByteData dataKey = await rootBundle.load('assets/ca/clientkey.pem');
Dio dio = Dio();
dio.interceptors.add(_interceptor);
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(client) {
SecurityContext serverContext = SecurityContext();
serverContext.useCertificateChainBytes(dataCRT.buffer.asUint8List());
serverContext.usePrivateKeyBytes(dataKey.buffer.asUint8List());
// use a new client for add the certificate and accept self signed certificate and return it
var newClient = HttpClient(context: serverContext);
newClient.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
return newClient;
};
logger.i("secureRequest");
logger.d("path: $path");
return await dio.request(path,
data: data,
queryParameters: queryParameters,
options: Options(
headers: headers,
method: method,
));
}
from dio.
Related Issues (20)
- Error Dio flutter web HOT 1
- upload file send timeout
- Header is out of order HOT 3
- `ResponseType.stream` is not working on web. HOT 1
- [🔔 Dio] sendTimeout cannot be used without a request body to send HOT 2
- `Transformer.isJsonMimeType` throws format exceptions without caught
- onError is not returning any error HOT 3
- Error when uploading image using MultipartFile.fromBytes in version 5.4.3+1 HOT 1
- 为什么dio的fromMap会将MultipartFile自动放入files字段中 HOT 1
- Error: The method 'debugLog' isn't defined for the class 'BrowserHttpClientAdapter'. HOT 1
- Version 5.5.0 brakes the web build HOT 1
- Is it now possible to use DIO / dio_web_adapter with Web/WASM? HOT 37
- Dio 5.5 broke compilation for WASM HOT 6
- ResponseType.stream returns responce all at once on Web HOT 1
- Date header not parsed correctly
- set proxy for BrowserHttpClientAdapter HOT 1
- Dio fetch can't catch error HOT 1
- Return the app to the desktop, then switch to Wi Fi in the system. After switching to Wi Fi, click on the desktop app icon to enter the app. At this point, there is an exception when sending network requests. This is highly likely to occur on Ios
- when using "handler.reject(error);" in onError of interceptors, I cannot DioException in .catchError(), but find it in .then().
- Dio cannot connect to the Internet after VPN is enabled on iOS devices
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dio.