选择 package:html、dart:html、dart:io (class HttpClient) 和 package:http API 来获取 HTTP 资源

Choosing between package:html, dart:html, dart:io (class HttpClient) and package:http APIs to fetch HTTP resources

我意识到目前至少有三个 "official" Dart 库允许我执行 HTTP 请求。更重要的是,其中三个库(dart:io(class HttpClient)、package:http 和 dart:html)各有一个不同的、不兼容的 API.

截至今天,package:html 不提供此功能,但在其 GitHub 页面上我发现它的目标是与 dart:html 100% API 兼容,所以这些方法最终会添加到那里。

哪个包提供了最未来的证明和平台独立性API以在 Dart 中发出 HTTP 请求?

是package:http吗?

import 'package:http/http.dart' as http;

var url = "http://example.com";
http.get(url)
    .then((response) {
  print("Response status: ${response.statusCode}");
  print("Response body: ${response.body}");
});

是飞镖吗:html/package:html?

import 'dart:html';

HttpRequest.request('/example.json')
  .then((response) {
      print("Response status: ${response.status}");
      print("Response body: ${response.response}");
});

或dart:io?

import 'dart:io';

var client = new HttpClient();
client.getUrl(Uri.parse("http://www.example.com/"))
    .then((HttpClientRequest request) {
      // Optionally set up headers...
      // Optionally write to the request object...
      // Then call close.
      ...
      return request.close();
    })
    .then((HttpClientResponse response) {
      print("Response status: ${response.statusCode}");
      print("Response body:");
      response.transform(UTF8.decoder).listen((contents) {
        print(contents);
      });
    });

假设我也想报道 Android。这也会在组合中添加 package:sky (https://github.com/domokit/sky_sdk/)。我承认这不是"official"Google库

import 'package:sky/framework/net/fetch.dart';

Response response = await fetch('http://example.com');
print(response.bodyAsString());

什么是(即将成为)常规产品是 https://www.youtube.com/watch?v=t8xdEO8LyL8。我想知道 他们的 HTTP 请求故事会是怎样的。有些东西告诉我,这将是另一种与我们迄今为止所见不同的野兽。

html 包是一个 HTML 解析器,它允许与 HTML 服务器端一起工作。我不希望它获得一些 HttpRequest 功能。

http 包旨在为客户端和服务器 Dart 代码提供统一的 API。 dart:html 中的 API 只是浏览器提供的 API 的包装。 dart:io 中的 HttpRequest API 是在没有浏览器限制的情况下构建的,因此偏离了 dart:htmlpackage:http 提供了一个统一的 API,它在浏览器中 运行 时委托给 dart:html,在服务器上 运行 时委托给 dart:io

我认为 package:http 是面向未来和跨平台的,应该很适合您的要求。