我想在控制台应用程序中使用静态 HttpRequest.getString
方法。但是,当导入 dart:html
来执行此操作时,我收到以下错误“内置库 'dart:html' 在独立 VM 上不可用”。我明白为什么,但我不确定替代方案是什么,因为我对这门语言非常陌生。
所以我的问题是如何在没有 dart:html
库的情况下创建 HttpRequest
?
一如既往,非常感谢任何帮助。
最佳答案
使用http
包。它有一个比 HttpClient
更好的 API 来发出 HTTP 请求。这是使用 http
的简单 GET 请求:
import 'package:http/http.dart' as http;
void main() {
var url = 'http://httpbin.org/';
http.get(url).then((response) {
print("Response status: ${response.statusCode}");
print("Response body: ${response.body}");
});
}
或者更简单地说:
import 'package:http/http.dart' as http;
void main() {
http.read("http://httpbin.org/").then(print);
}
更多示例:
https://github.com/dart-lang/code_snippets/tree/master/recipes/dart_io/pkg_http/bin
关于dart - 不使用 dart 发出 HTML 请求 :html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20433256/