我正在寻找在 Dart 中获得类似 curl 功能的最佳方法。例如,如何获取 google.com 网络内容并输出它,作为示例。
我发现我可以通过 the shell as shown here 调用它,但这似乎不是理想的方法:
import 'dart:io';
main() {
var f = new File(new Options().executable);
Process.start('curl',
['--dump-header', '/tmp/temp_dir1_M8KQFW/curl-headers', '--cacert',
'/Users/ager/dart/dart/third_party/curl/ca-certificates.crt', '--request',
'POST', '--data-binary', '@-', '--header', 'accept: ', '--header', 'user-agent: ' ,
'--header', 'authorization: Bearer access token', '--header',
'content-type: multipart/form-data', '--header',
'content-transfer-encoding: binary', '--header',
'content-length: ${f.lengthSync()}', 'http://localhost:9000/upload']).then((p) {
f.openInputStream().pipe(p.stdin);
p.stdout.pipe(stdout);
p.stderr.pipe(stderr);
p.onExit = (e) => print(e);
});
}
我还查看了 API,在这里找不到任何可以帮助我的东西。
最佳答案
Dart IO 库带有 HttpClient
这基本上就是你要找的。但是,您可能应该使用 http
改为发布包。将其添加到您的依赖项文件中:
dependencies:
http: any
运行
pub install
然后只是:import 'package:http/http.dart' as http;
main() {
http.read('http://google.com').then((contents) {
print(contents); // Here we output the contents of google.com.
});
}
关于dart - Dart 中的 curl 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14013069/