我意识到目前至少有三个“官方”Dart 库允许我执行 HTTP 请求。更重要的是,其中三个库(dart:io(类 HttpClient)、package:http 和 dart:html)各自具有不同的、不兼容的 API。
截至今天,package:html 尚未提供此功能,但在其 GitHub 页面上我发现它的目标是与 dart:html 100% API 兼容,因此最终将在其中添加这些方法。
哪个包提供了最面向未来且独立于平台的 API 来在 Dart 中发出 HTTP 请求?
是包: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}");
});
是 dart:html/package:html 吗?
import 'dart:html';
HttpRequest.request('/example.json')
.then((response) {
print("Response status: ${response.status}");
print("Response body: ${response.response}");
});
或者飞镖: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/ https://github.com/domokit/sky_sdk/)。我承认这不是“官方”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 https://www.youtube.com/watch?v=t8xdEO8LyL8。我想知道theirHTTP 请求的故事将会是这样。有件事告诉我,这将是另一种与我们迄今为止所见过的不同的野兽。