我意识到目前至少有三个“官方”Dart库允许我执行HTTP请求。 更重要的是,其中三个库(dart:io(类HttpClient),包: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。 这也增加了包装:天空( https://github.com/domokit/sky_sdk/ )。 我承认这不是“官方”谷歌图书馆。
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请求故事是什么。 有些东西告诉我,到目前为止,它将是我们所见过的另一种不同的野兽。