【问题标题】:Curl like functionality in DartDart 中的卷曲功能
【发布时间】:2012-12-23 17:33:23
【问题描述】:

我正在寻找在 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


    【解决方案1】:

    Dart IO 库带有 HttpClient,这基本上就是您要寻找的。但是,您可能应该改用http Pub 包。将其添加到您的依赖项文件中:

    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.
      });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-02
      • 2022-01-07
      • 2013-02-05
      • 2020-10-17
      • 2017-03-27
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多