【发布时间】:2020-11-21 11:54:58
【问题描述】:
我想向相机发送一个 POST 请求并接收一个 motionJPEG 流返回。我正在使用 Dart http 包。据我所知,我不能使用http.post 接收流作为响应。我正在尝试使用http.Client.send。我不知道如何为http.Request 创建正确的正文和标题。
大多数 IP 摄像机使用 GET 来访问 MotionJPEG 字节流。但是,我使用的相机是 RICOH THETA 相机,它需要一个带有有效负载的 POST 命令才能发送到相机。如果有人知道我如何使用 dart http 模块创建正确的 POST 请求以返回带有标题和正文的流,请帮助。
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';
main() async {
Uri url = Uri.parse('https://192.168.1.1/osc/commands/execute');
var request = http.Request('POST', url);
Map<String, String> bodyMap = {'name': 'camera.getLivePreview'};
request.body = jsonEncode(bodyMap);
Map<String, String> headers = {"Content-type": "application/json"};
http.Client client = http.Client();
StreamSubscription videoStream;
client.head(url, headers: headers);
client.send(request).then((response) {
var startIndex = -1;
var endIndex = -1;
List<int> buf = List<int>();
videoStream = response.stream.listen((List<int> data) {
for (var i = 0; i < data.length; i++) {
print(data[i]);
}
});
});
}
【问题讨论】: