【发布时间】:2022-01-17 04:11:15
【问题描述】:
我正在构建一个使用外部服务的应用程序,该服务提供链接到 PDF 文档的 URL 列表。对于其中一些 URL,资源不存在 - 404。
我尝试构建以下内容以遍历 URL 列表并获取资源是否存在,但它运行缓慢 - 每个链接大约 100 毫秒到 500 毫秒秒。
任何人有任何建议如何快速判断资源是否存在?
var client = http.Client();
for (Article article in articles) {
if (article.downloadUrl != null) {
Uri url = Uri.parse(article.downloadUrl!);
http.Response _response = await client.head(url);
if (_response.statusCode == 404) {
print('Bad - ${article.downloadUrl}');
} else {
print('Good - ${article.downloadUrl}');
}
}
}
【问题讨论】: