问题描述
我意识到,目前至少有三个官方" Dart库允许我执行HTTP请求.而且,其中的三个库(dart:io(HttpClient类),package:http和dart:html)分别具有不同的不兼容API.
I realized that currently there are at least three "official" Dart libraries that allow me to perform a HTTP request. What is more, three of those libraries (dart:io (class HttpClient), package:http and dart:html) have each a different, incompatible API.
到目前为止,package:html尚不提供此功能,但是在其GitHub页面上,我发现它旨在与dart:html实现100%API兼容性,因此最终将在其中添加这些方法.
As of today, package:html does not offer this functionality, but on its GitHub page I found it aims for 100% API compatibility with dart:html, so these methods will be added there eventually.
哪个程序包提供了最新的,与平台无关的API,可以在Dart中发出HTTP请求?
Which package provides the most future proof and platform independent API to issue a HTTP request in Dart?
是包: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吗?
Is it dart: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/).我承认这不是官方" Google图书馆.
Let's say I want to cover Android too. That adds package:sky in the mix as well (https://github.com/domokit/sky_sdk/). I admit that this is not "official" Google library.
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请求的故事是什么.某些事情告诉我,这将是迄今为止我们所见不到的另一种野兽.
What is (going to be) a regular product is https://www.youtube.com/watch?v=t8xdEO8LyL8. I wonder what their HTTP Request story is going to be. Something tells me it will be yet another different beast from all we have seen so far.
推荐答案
html
包是HTML解析器,允许与HTML服务器端一起使用.我不希望它获得一些HttpRequest功能.
The html
package is a HTML parser which allows to work with HTML server side. I wouldn't expect it to get some HttpRequest capabilities.
http
软件包旨在为客户端和服务器Dart代码提供统一的API. dart:html
中的API只是浏览器提供的API的包装. dart:io
中的HttpRequest API的构建不受浏览器限制,因此与dart:html
不同. package:http
提供了一个统一的API,当在浏览器中运行时,它委派给dart:html
;而在服务器上运行时,委派给dart:io
.
The http
package aims to provide a unified API for client and server Dart code. The API in dart:html
is only a wrapper over the API the browser provides. The HttpRequest API in dart:io
was built without browser restrictions and thus deviates from dart:html
. package:http
provides an unified API which delegates to dart:html
when run in the browser and to dart:io
when run on the server.
我认为package:http
是面向未来和跨平台的,应该非常适合您的要求.
I think package:http
is future proof and cross-platform and should be a good fit for your requirements.
这篇关于在package:html,dart:html,dart:io(HttpClient类)和package:http API之间进行选择以获取HTTP资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!