[英]Using signalr_client package in flutter application, setting authorization header. FormatException: Invalid HTTP header field name
我正在尝试将我的 flutter 应用程序连接到我的 .net 核心应用程序中的集线器。
这就是我正在尝试的:
// Connection to the signalr Hub
_connectToHub() async {
// The location of the SignalR Server.
final serverUrl = "https://192.168.1.65:5001/notification";
// Creates the connection by using the HubConnectionBuilder.
final hubConnection = HubConnectionBuilder().withUrl(serverUrl,
options: new HttpConnectionOptions(accessTokenFactory: () async {return await FlutterSession().get("token");}))
.build();
await hubConnection.start().then((value){print("CONNECTED TO HOST HUB");}).onError((error, stackTrace){
print(stackTrace);
print(error);
});
hubConnection.on("simo", _invokedSimo);
// When the connection is closed, print out a message to the console.
hubConnection.onclose( (error) => print("Connection Closed"));
}
这是堆栈跟踪:
I/flutter (15069): #0 HttpConnection._startInternal (package:signalr_client/http_connection.dart:255:7) I/flutter (15069): I/flutter (15069): #1
HubConnection.start (package:signalr_client/hub_connection.dart:103:5) I/flutter (15069): I/flutter (15069): #2
FutureExtensions.onError。 (dart:async/future.dart) I/flutter (15069): I/flutter (15069): FormatException: 无效 HTTP header 字段名称:“承载 eyJhbGciOiJ..
经过一些研究,这是 package 的代码中断的地方:
final response = await _httpClient.post(negotiateUrl, options: options);
谢谢您的帮助
为了解决这个问题,我不得不修改 package 本身的一行:
在文件 http_connection.dart 中:方法: _getNegotiationResponse
if (_accessTokenFactory != null) {
final token = await _accessTokenFactory();
if (token != null) {
headers.setHeaderValue("Authorization", "$token");
}
}
而不是“Bearer $token”....我猜是因为这个 function 不支持带有空格字符的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.