Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Error with non signed certificates #9

Closed
julioffigueiredo opened this issue Sep 28, 2021 · 1 comment
Closed

Error with non signed certificates #9

julioffigueiredo opened this issue Sep 28, 2021 · 1 comment

Comments

@julioffigueiredo
Copy link

  • Using with platform: WEB
  • dio_http: ^5.0.4
  • Flutter: 2.5

Same request with non signed certificate that in windows, ios, android returns ok, mac not tested, with web project get this error:

Error: Expected a value of type 'DefaultHttpClientAdapter', but got one of type 'BrowserHttpClientAdapter'

This is the code used to avoid certificate error, as I said, work for Androis, iOS and Windows (not tested for Mac):

  (_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
      (HttpClient client) {
    client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
    return client;
  };
@jgoyvaerts
Copy link
Contributor

This is not a bug. Depending on the platform, dio uses a different adapter (on Web it uses a BrowserHttpClientAdapter, on all other platforms it uses a DefaultHttpClientAdapter).

You are casting your adapter to DefaultHttpClientAdapter but it's actually a BrowserHttpClientAdapter, so your code gives an error. Before casting you should check if it is a DefaultHttpClientAdapter.

if(_dio.httpClientAdapter is DefaultHttpClientAdapter){
//do your code
} else {
//do something else
}

Keep in mind, I don't think it's pssible to ignore certificate errors on web programmatically, this is a browser limitation, not a dio/dart limitation. It should work on web if your website is on the same domain as the api you are calling.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants