Dart / Flutter POST请求和流响应

时间:2020-07-31 15:53:19

标签: flutter dart

我想向相机发送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]);
      }
    });
  });
}

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,实际上您的操作是正确的,但是return软件包修改了您的请求并为其添加了一些额外的细节,因此请使用on run {input, parameters} set output to {} repeat with this_item in input set new_item to this_item -- ... obviously you'd do something other than just copy copy new_item to end of output end repeat return output end run

示例代码

HTTP

为更加清晰起见,请转到this stackoverflow帖子

答案 1 :(得分:0)

这是如何发送信息和接收信息流的摘要。就我而言,我正在返回视频流并提取帧。在摘要中,我提取了10帧进行测试。

仍然可以使用dart http模块,但是我还没有找到解决方法。使用HttpClient对我有效,并且在易用性方面大致相同。

import 'dart:async';
import 'dart:io';
import 'dart:convert';

Uri apiUrl = Uri.parse('http://192.168.1.1/osc/commands/execute');

Map payload = {'name': 'camera.getLivePreview'};

void main() async {
  var client = HttpClient();
  StreamSubscription videoStream;

  var request = await client.postUrl(apiUrl)
    ..headers.contentType = ContentType.json
    ..write(jsonEncode(payload));

  HttpClientResponse response = await request.close();

  var startIndex = -1;
  var endIndex = -1;

  // frame buffer for a single frame
  List<int> buf = List<int>();
  int counter = 0;

  Duration ts = null;
  Stopwatch timer = Stopwatch();

  timer.start();

// set up list of frames
  var frameList = [];
  for (int frameCount = 0; frameCount < 10; frameCount++) {
    frameList.add(await new File('frame$frameCount.jpg'));
  }

  //  Handle the response
  var resStream = response.listen(
    (List<int> data) {
      for (var i = 0; i < data.length - 1; i++) {
        if (data[i] == 0xff && data[i + 1] == 0xd8) {
          startIndex = buf.length + i;
        }
        if (data[i] == 0xff && data[i + 1] == 0xd9) {
          endIndex = buf.length + i;
        }
      }
      buf.addAll(data);


相关问题