问题描述
我正在使用Flutter通过https连接Java Java服务器实现.我首先测试了它是否可以仅使用http.
I am using flutter to connect with java java server implementation over https. I first tested it to be working using just http.
然后我在服务器端切换到https,并将其指向我使用keytool创建的自签名证书.
I then switched to https on the server side and pointed it at my self signed certificate I created using keytool.
然后,我尝试使用http dart软件包连接到它.结果导致以下异常...
Then I tried to connect to it using the http dart package. The resulted in the following exception...
我假设我需要设置客户端信任我的服务器自签名证书.我查看了APi参考,但不知道如何实现此目的...
I am assuming I need to set my client to trust my servers self signed certificate. I have looked at the APi reference and could not figure out how to get this to happen...
在我的flutter应用程序中,我的飞镖代码如下...
My dart code in my flutter app is as follows...
void testMessage() {
var url = 'https://192.168.100.105:8443';
var response = await http.post(url, body: "{\"message_name\": \"TestMessage\", \"contents\": { \"field1\":\"blah\", \"field2\":\"blah\" }}");
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
}
推荐答案
在开发过程中,您可以使用HttpClient的 badCertificateCallback
回调,而只需返回 true
.这将接受所有错误的证书.
While developing you can use the badCertificateCallback
callback of HttpClient and just return true
. This will accept all bad certificates.
HttpClient client = HttpClient()
..badCertificateCallback = ((X509Certificate cert, String host, int port) => true);
要接受特定的不良证书,您可以在此处尝试以下代码: https://github.com/dart-lang/http/issues/14#issuecomment-311184690
To accept a specific bad certificate you may experiment with this code from here: https://github.com/dart-lang/http/issues/14#issuecomment-311184690
import 'dart:io';
import 'package:http/http.dart' as http;
bool _certificateCheck(X509Certificate cert, String host, int port) =>
host == 'local.domain.ext'; // <- change
HttpClient client = new HttpClient()
..badCertificateCallback = (_certificateCheck);
这篇关于带有自签名证书的Flutter https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!