使用Http绕过证书错误

时间:2013-05-13 14:34:20

标签: 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));

,这是返回的错误:

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)

2 个答案:

答案 0 :(得分:0)

错误-8172表示'Peer的证书颁发者已被标记为不被用户信任。'

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

此外,对于服务器套接字,您不能依赖隐式SecureSocket.initialize()调用。您需要call it explicitly with certificate db info

答案 1 :(得分:-1)

尽管这个问题可能已经过时,但如果有人遇到同样的麻烦,我仍然应该对此作一个简短的回答。这个片段来自我的项目:

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);
...

here中提到了该问题以及解决方法。