如何将 badCertificateCallback 添加到 http.BaseClient [Flutter, Dart]
How to add badCertificateCallback to the http.BaseClient [Flutter, Dart]
我正在使用 signalr_core 连接到 signalr。
我创建了如下代码,这个连接没有问题。
SignalRNotification() {
if (this._hubConnection == null) {
var options =
IOClient(HttpClient()..badCertificateCallback = (x, y, z) => true);
this._hubConnection = HubConnectionBuilder()
.withUrl(serverUrl, options)
.withAutomaticReconnect()
.build();
}
this._initHub();
}
我想修改选项里的header信息
我添加了一个 header 引用 this article. 但是,出现 CERTIFICATE_VERIFY_FAILED 错误。
E/flutter ( 2902): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: HandshakeException: Handshake error in client (OS Error:
E/flutter ( 2902): CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(handshake.cc:354))
这是我的代码。
import 'dart:io';
import 'package:http/io_client.dart';
import 'package:signalr_core/signalr_core.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/foundation.dart';
import '../resources/common.dart';
class _HttpClient extends http.BaseClient {
final _httpClient = http.Client();
final Map<String, String> defaultHeaders;
_HttpClient({@required this.defaultHeaders});
@override
Future<http.StreamedResponse> send(http.BaseRequest request) {
request.headers.addAll(defaultHeaders);
return _httpClient.send(request);
}
}
class SignalRNotification {
static String serverUrl = SignalRURL;
HubConnection _hubConnection;
NotificationManager notificationManager = NotificationManager();
final httpClient = _HttpClient(defaultHeaders: {
"X-Api-Key": "key",
"X-Client-Id": "id",
});
SignalRNotification() {
if (this._hubConnection == null) {
this._hubConnection = HubConnectionBuilder()
.withUrl(serverUrl, HttpConnectionOptions(client: httpClient))
.withAutomaticReconnect()
.build();
}
this._initHub();
}
我认为出现这个问题是因为_HttpClient中没有badCertificateCallback
我也参考了this article,但是没用。
我想知道解决方案。
来自 dart:io
的 HttpClient
(我将其称为 io.HttpClient
)提供了 badCertificateCallback
setter 但 package:http
的 BaseClient
确实没有。
但是,package:http
为其 BaseClient
class 提供了几种实现。其中之一是 IOClient
(我将其称为 http.IOClient
),它由 io.HttpClient
.
支持
因此,您应该能够将 http.IOClient
与按您希望的方式配置的 io.HttpClient
一起使用,例如:
import 'dart:io' as io;
import 'package:http/http.dart' as http;
import 'package:http/io_client.dart' as http;
class _HttpClient extends http.BaseClient {
final io.HttpClient _ioHttpClient;
final http.IOClient _httpClient;
final Map<String, String> defaultHeaders;
_HttpClient({@required this.defaultHeaders})
: _ioHttpClient = io.HttpClient() {
_httpClient = http.IOClient(_ioHttpClient);
}
void set badCertificateCallback(BadCertificateCallback callback) =>
_ioHttpClient.badCertificateCallback = callback;
@override
Future<http.StreamedResponse> send(http.BaseRequest request) {
request.headers.addAll(defaultHeaders);
return _httpClient.send(request);
}
}
我正在使用 signalr_core 连接到 signalr。 我创建了如下代码,这个连接没有问题。
SignalRNotification() {
if (this._hubConnection == null) {
var options =
IOClient(HttpClient()..badCertificateCallback = (x, y, z) => true);
this._hubConnection = HubConnectionBuilder()
.withUrl(serverUrl, options)
.withAutomaticReconnect()
.build();
}
this._initHub();
}
我想修改选项里的header信息
我添加了一个 header 引用 this article. 但是,出现 CERTIFICATE_VERIFY_FAILED 错误。
E/flutter ( 2902): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: HandshakeException: Handshake error in client (OS Error:
E/flutter ( 2902): CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(handshake.cc:354))
这是我的代码。
import 'dart:io';
import 'package:http/io_client.dart';
import 'package:signalr_core/signalr_core.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/foundation.dart';
import '../resources/common.dart';
class _HttpClient extends http.BaseClient {
final _httpClient = http.Client();
final Map<String, String> defaultHeaders;
_HttpClient({@required this.defaultHeaders});
@override
Future<http.StreamedResponse> send(http.BaseRequest request) {
request.headers.addAll(defaultHeaders);
return _httpClient.send(request);
}
}
class SignalRNotification {
static String serverUrl = SignalRURL;
HubConnection _hubConnection;
NotificationManager notificationManager = NotificationManager();
final httpClient = _HttpClient(defaultHeaders: {
"X-Api-Key": "key",
"X-Client-Id": "id",
});
SignalRNotification() {
if (this._hubConnection == null) {
this._hubConnection = HubConnectionBuilder()
.withUrl(serverUrl, HttpConnectionOptions(client: httpClient))
.withAutomaticReconnect()
.build();
}
this._initHub();
}
我认为出现这个问题是因为_HttpClient中没有badCertificateCallback
我也参考了this article,但是没用。 我想知道解决方案。
dart:io
的 HttpClient
(我将其称为 io.HttpClient
)提供了 badCertificateCallback
setter 但 package:http
的 BaseClient
确实没有。
但是,package:http
为其 BaseClient
class 提供了几种实现。其中之一是 IOClient
(我将其称为 http.IOClient
),它由 io.HttpClient
.
因此,您应该能够将 http.IOClient
与按您希望的方式配置的 io.HttpClient
一起使用,例如:
import 'dart:io' as io;
import 'package:http/http.dart' as http;
import 'package:http/io_client.dart' as http;
class _HttpClient extends http.BaseClient {
final io.HttpClient _ioHttpClient;
final http.IOClient _httpClient;
final Map<String, String> defaultHeaders;
_HttpClient({@required this.defaultHeaders})
: _ioHttpClient = io.HttpClient() {
_httpClient = http.IOClient(_ioHttpClient);
}
void set badCertificateCallback(BadCertificateCallback callback) =>
_ioHttpClient.badCertificateCallback = callback;
@override
Future<http.StreamedResponse> send(http.BaseRequest request) {
request.headers.addAll(defaultHeaders);
return _httpClient.send(request);
}
}