flutter - 如何使 Flutter HTTP 部分(范围)请求?
问题描述
代码
我有这个main.dart
颤振代码:
import 'package:http/http.dart' as $http;
Future main() async {
await hit();
}
Future hit() async {
const url = 'https://data.wikibulary.com/data/plain/index/en/0.bin';
const len = 10000024;
final resp = await $http.get(url, headers: {
'range': 'bytes = ${len - 1}-${len - 1}',
'cache-control': 'no-cache',
});
final headers = resp.headers.entries.fold('', (r, i) => '$r\n${i.key}: ${i.value}');
print('length: ${resp.bodyBytes.length}\n-------------\nheaders:\n$headers');
}
问题
Windows 桌面和 Android 将忽略范围请求标头。对于 Flutter Web,它工作得很好。
所有三个平台的响应标头:
网站:statusCode: 206
length: 1
-----------
cache-control: public, max-age=86400000
content-length: 1
content-type: application/octet-stream
date: Sat, 09 Jan 2021 11:12:22 GMT
etag: 0x8D8B4749CC587E6
last-modified: Sat, 09 Jan 2021 08:00:21 GMT
视窗:
statusCode: 200
length: 10000024
-----------
connection: keep-alive
last-modified: Sat, 09 Jan 2021 08:00:21 GMT
cache-control: public, max-age=86400000
access-control-allow-origin: *
date: Sat, 09 Jan 2021 11:09:00 GMT
vary: Accept-Encoding
age: 6367
content-type: application/octet-stream
accept-ranges: bytes
content-length: 10000024
etag: 0x8D8B4749CC587E6
安卓模拟器:
I/flutter ( 5528): statusCode: 200
I/flutter ( 5528): length: 10000024
I/flutter ( 5528): -----------
I/flutter ( 5528):
I/flutter ( 5528): connection: keep-alive
I/flutter ( 5528): last-modified: Sat, 09 Jan 2021 08:00:21 GMT
I/flutter ( 5528): cache-control: public, max-age=86400000
I/flutter ( 5528): access-control-allow-origin: *
I/flutter ( 5528): date: Sat, 09 Jan 2021 11:10:42 GMT
I/flutter ( 5528): vary: Accept-Encoding
解决方案
问题解决了,空间字符在'bytes = ${len - 1}-${len - 1}'
正确的代码是:
final resp = await $http.get(url, headers: {
'range': 'bytes=${len - 1}-${len - 1}',
'cache-control': 'no-cache',
});
推荐阅读
- php - 用户如何对价格进行排序?
- c# - 无法隐式转换类型泛型列表的错误
- oauth-2.0 - 避免暴露 Keycloak
- visual-studio - 实例化不会停止在 count 并添加一个额外的。解决方案?
- javascript - 将curveStep应用于d3 forceDirected布局中的路径
- javascript - 带黑盒数据库的管理软件
- wordpress - Revolution Slider 6.2.23 中的滑块图像不适用于所有设备
- python - Matplotlib 条形图自动标签子图 - 名称“轴”无法识别
- javascript - 无法在自定义 BetterDiscord 扩展代码中解构“TouchBar”的属性“TouchBarButton”
- informatica - 使用来自 wsdl 的数据并通过 Informatica 云将数据公开为 Rest 或 soap,以供 Salesforce 应用程序使用