使用Http绕过证书错误

Adi*_*dha 5 https dart

我正在尝试创建访问第三方API的代理服务器,但是它们的开发终点存在证书错误。无论如何,使用http.dart时会绕过ssl错误吗?

import 'package:http/http.dart' as http;

Uri url = Uri.parse("https://url-with-ssl-error.com/endpoint");
http.get(url).then((response) => print(response.body));
Run Code Online (Sandbox Code Playgroud)

这是返回的错误:

Uncaught Error: SocketIOException: RawSecureSocket error (Unexpected handshake error in client) (OS Error: errno = -8172)
Unhandled exception:
SocketIOException: RawSecureSocket error (Unexpected handshake error in client) (OS Error:  errno = -8172)
#0      _FutureImpl._scheduleUnhandledError.<anonymous closure> (dart:async/future_impl.dart:207:9)
#1      Timer.run.<anonymous closure> (dart:async/timer.dart:17:21)
#2      Timer.run.<anonymous closure> (dart:async/timer.dart:25:13)
#3      Timer.Timer.<anonymous closure> (dart:async-patch:15:15)
#4      _Timer._createTimerHandler._handleTimeout (dart:io:6990:28)
#5      _Timer._createTimerHandler._handleTimeout (dart:io:6998:7)
#6      _Timer._createTimerHandler.<anonymous closure> (dart:io:7006:23)
#7      _ReceivePortImpl._handleMessage (dart:isolate-patch:81:92)
Run Code Online (Sandbox Code Playgroud)

nđq*_*nđq 5

虽然这个问题可能已经过时了,但我仍然应该为此发布一个简短的答案,以防有人遇到同样的麻烦。此片段来自我的项目:

import 'package:http/http.dart' as http;
import 'package:http/io_client.dart';
...

HttpClient client = new HttpClient()..badCertificateCallback = ((X509Certificate cert, String host, int port) => true);
var ioClient = new IOClient(client);
http.Response resp = await ioClient.post(uri, body: utf8.encode(json.encode(body)), headers: headers);
...
Run Code Online (Sandbox Code Playgroud)

这里提到了这个问题以及解决方案。


Zde*_*vic 0

错误 -8172 意味着“对等方的证书颁发者已被标记为不受用户信任。”

如果您有权访问原始套接字,则connect方法允许您通过提供回调来指定在证书错误时要执行的操作onBadCertificate。但是,我不确定http代码示例中对象的确切类型是什么,因此我无法判断您是否可以解决此问题。我认为它可能是一个HttpClient实例,但它没有get采用 URI 的方法,所以我不确定。如果它是您自己的类,也许您可​​以访问底层安全套接字,因此您仍然可以使用onBadCertificate.

此外,对于服务器套接字,您不能依赖隐式SecureSocket.initialize()调用。您需要使用证书数据库信息显式调用它