我正在寻找在Dart中获得类似卷曲功能的最佳方法.例如,如何获取google.com网络内容并输出它,作为示例.
我发现我可以通过shell调用它,如图所示,但这似乎不是理想的方法:
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);
});
}
Run Code Online (Sandbox Code Playgroud)
我也查看了API,在这里找不到任何帮助我的东西.
Dart IO库附带了一个HttpClient基本上你正在寻找的东西.但是,您应该使用httpPub包.将其添加到您的依赖项文件:
dependencies:
http: any
Run Code Online (Sandbox Code Playgroud)
运行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.
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1316 次 |
| 最近记录: |