对于没有证书的HTTPS站点,尝试使用NetworkImage加载图像失败并带有CERTIFICATE_VERIFY_FAILED [英] Trying to load an image using NetworkImage fails with CERTIFICATE_VERIFY_FAILED for HTTPS site with no certificate
问题描述
在Flutter中,尝试使用 NetworkImage -引发抛出异常: 客户端握手错误(操作系统错误:CERTIFICATE_VERIFY_FAILED:确定(handshake.cc:355))
In Flutter, trying to load an image from a (development) server (HTTPS without a certificate), using NetworkImage - results in an exception thrown: Handshake error in client (OS Error: CERTIFICATE_VERIFY_FAILED: ok(handshake.cc:355))
是否有解决方法,类似于 HTTPClient ? httpClient.badCertificateCallback =(X509Certificate cert,字符串主机,int端口)=> true;
Is there a workaround for this, similar to how it's done in HTTPClient? httpClient.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
推荐答案
这是给以后出现在这里的人的.我只是用@Alexey(他给出的出色解决方案)所述的NetworkImageSSL的完整代码来帮助其他人.
This is for someone who appears here later. I'm just helping others with the full code of NetworkImageSSL as stated by @Alexey (Excellent solution given by him).
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui show instantiateImageCodec, Codec;
import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart';
class NetworkImageSSL extends ImageProvider<NetworkImageSSL> {
const NetworkImageSSL(this.url, {this.scale = 1.0, this.headers})
: assert(url != null),
assert(scale != null);
final String url;
final double scale;
final Map<String, String> headers;
@override
Future<NetworkImageSSL> obtainKey(ImageConfiguration configuration) {
return new SynchronousFuture<NetworkImageSSL>(this);
}
@override
ImageStreamCompleter load(NetworkImageSSL key, DecoderCallback decode) {
return MultiFrameImageStreamCompleter(codec: _loadAsync(key), scale: key.scale);
}
static final HttpClient _httpClient = new HttpClient()
..badCertificateCallback =
((X509Certificate cert, String host, int port) => true);
Future<ui.Codec> _loadAsync(NetworkImageSSL key) async {
assert(key == this);
final Uri resolved = Uri.base.resolve(key.url);
final HttpClientRequest request = await _httpClient.getUrl(resolved);
headers?.forEach((String name, String value) {
request.headers.add(name, value);
});
final HttpClientResponse response = await request.close();
if (response.statusCode != HttpStatus.ok)
throw new Exception(
'HTTP request failed, statusCode: ${response?.statusCode}, $resolved');
final Uint8List bytes = await consolidateHttpClientResponseBytes(response);
if (bytes.lengthInBytes == 0)
throw new Exception('NetworkImageSSL is an empty file: $resolved');
return await ui.instantiateImageCodec(bytes);
}
@override
bool operator ==(dynamic other) {
if (other.runtimeType != runtimeType) return false;
final NetworkImageSSL typedOther = other;
return url == typedOther.url && scale == typedOther.scale;
}
@override
int get hashCode => hashValues(url, scale);
@override
String toString() => '$runtimeType("$url", scale: $scale)';
}
这篇关于对于没有证书的HTTPS站点,尝试使用NetworkImage加载图像失败并带有CERTIFICATE_VERIFY_FAILED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!