0

我正在尝试将客户端证书从http.dart包加载到 http.client。

我已经看到了关于如何使用HttpClient类的多个答案,例如这个答案:Flutter add self signed certificate from assets folder,这基本上建议执行以下代码

ByteData data = await rootBundle.load('assets/raw/certificate.crt');
SecurityContext context = SecurityContext.defaultContext;
context.useCertificateChainBytes(data.buffer.asUint8List());
client = HttpClient(context: context);

但我必须使用http.dart包,因为我有一个函数可以接受这样的http.client 东西

import 'package:http/http.dart' as http;

var httpClient = http.Client();
// i'd like to configure this httpClient to use a specific client certificate

var client = MyClient(httpClient);

....

MyClient (http.Client? httpClient) {
    -- some constructor logic --
}

有没有办法配置 ahttp.client以使用客户端证书?

谢谢。

4

1 回答 1

2

不要使用http.Client()构造函数。相反,构造一个IOClient(它是Clientas 的子类,可以代替使用)。传入你的HttpClient.

import 'dart:io';

import 'package:http/io_client.dart';

void main() async {
  final context = SecurityContext.defaultContext;
  // modify context as needed
  final httpClient = HttpClient(context: context);
  final client = IOClient(httpClient);

  await client.get(Uri.parse('https://somewhere.io'));
}
于 2022-01-17T13:50:57.390 回答