将标头添加到SecureSocket或将SecurityContext添加到WebSpcket [英] Add headers to a SecureSocket or SecurityContext to WebSpcket
问题描述
WebSocket可以选择向请求添加标头,但不能向其添加SecurityContext. SecureSocket具有SecurityContext,因此我可以添加ssl证书,但不能添加标头.有什么想法吗?
The WebSocket has the option to add a headers to the request but you can't add a SecurityContext to it. The SecureSocket has the SecurityContext so I can add the ssl certs but can't add headers. Any idea on this?
更新: 我正在尝试使用SecurityContext对象创建一个websocket 由于我无权访问底层的HttpClient,因此我尝试使用 runZoned方法
UPDATE: I'm trying to create a websocket with a SecurityContext object As I don't have access to the underling HttpClient I've tried to use the runZoned method
int i = 0;
socket = await io.HttpOverrides.runZoned<Future<io.WebSocket>>(
() => io.WebSocket.connect(uri, headers: headers),
createHttpClient: (io.SecurityContext securityContext) {
log.w('creating HttpClient ${i++}');
return new io.HttpClient(context: options.securityContext);
},
);
createHttpClient被调用了3550次 然后什么也没发生
the createHttpClient is called some 3550 times and then nothing happens
这应该非常简单,因为websocket httpclient只是放置在其中
It should be very simple because the websocket httpclient just lays there
推荐答案
尝试一下
class MyHttpOverrides extends HttpOverrides {
@override
HttpClient createHttpClient(SecurityContext context) {
return super.createHttpClient(context)
..badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
}
}
// Run this code in main function
HttpOverrides.global = new MyHttpOverrides();
这篇关于将标头添加到SecureSocket或将SecurityContext添加到WebSpcket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!