【问题标题】:Using signalr_client package in flutter application, setting authorization header. FormatException: Invalid HTTP header field name在 Flutter 应用中使用 signalr_client 包,设置授权头。 FormatException:HTTP 标头字段名称无效
【发布时间】:2022-01-19 18:23:03
【问题描述】:

我正在尝试将我的 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 (包:signalr_client/http_connection.dart:255:7) I/flutter(15069): 我/颤振(15069):#1
HubConnection.start (package:signalr_client/hub_connection.dart:103:5) I/颤动 (15069): I/颤动 (15069): #2
FutureExtensions.onError。 (dart:async/future.dart) I/颤动(15069): I/颤动(15069): FormatException:无效的 HTTP 标头字段名称:“Bearer eyJhbGciOiJ..

经过一点研究,这是包代码中断的地方:

final response = await _httpClient.post(negotiateUrl, options: options);

感谢您的帮助

【问题讨论】:

    标签: .net flutter signalr webapi


    【解决方案1】:

    为了解决这个问题,我不得不修改包本身的一行:

    在文件 http_connection.dart 中:方法:_getNegotiationResponse

    if (_accessTokenFactory != null) {
          final token = await _accessTokenFactory();
          if (token != null) {
            headers.setHeaderValue("Authorization", "$token");
          }
        }
    

    而不是“不记名$token” .... 我猜是因为这个函数不支持带空格字符的字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-14
      • 2017-08-18
      • 2017-08-30
      • 2021-11-21
      • 2015-12-02
      • 2011-03-03
      相关资源
      最近更新 更多