How to Bypass SSL Certificate Verification in flutter?
3,844
You need to configure your HttpService to work with Self-Signed SSL local servers. Like this:
import 'dart:io';
import 'dart:convert';
class HttpService {
Future<dynamic> sendRequestToServer(dynamic model, String reqType, bool isTokenHeader, String token) async {
HttpClient client = new HttpClient();
client.badCertificateCallback =((X509Certificate cert, String host, int port) => true);
HttpClientRequest request = await client.postUrl(Uri.parse("https://${serverConstants.serverUrl}$reqType"));
request.headers.set('Content-Type', 'application/json');
if(isTokenHeader){
request.headers.set('Authorization', 'Bearer $token');
}
request.add(utf8.encode(jsonEncode(model)));
HttpClientResponse result = await request.close();
if(result.statusCode == 200) {
return jsonDecode(await result.transform(utf8.decoder)
.join());
} else {
return null;
}
}
}
Read more from here.
![Haroon Ahmed](https://i.stack.imgur.com/M6vtf.jpg?s=256&g=1)
Author by
Haroon Ahmed
Updated on December 25, 2022Comments
-
Haroon Ahmed over 1 year
How to Bypass SSL Certificate Verification in flutter? Error: Handshake Exception: Handshake error in client(OS Error:CERTIFICATE_VERIFY_FAILED:self signed certificate(handshake.cc:345)
-
Amroun over 3 yearsCheck this post: stackoverflow.com/questions/54285172/…
-
-
Haroon Ahmed over 3 yearshow can call this function?
-
Dani3le_ about 2 yearsWhile this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review