像Dart中的Curl类似的功能 [英] Curl like functionality in Dart

查看:163
本文介绍了像Dart中的Curl类似的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在Dart中获得类curl功能的最佳方法。例如,如何获取google.com网络内容并输出。

I'm looking for the best way to get curl-like functionality in Dart. For example, how to fetch the google.com web content and output it, as an example.

我发现我可以通过如此处所示的shell ,然而这似乎不是理想的方法:

I found that I can call it via the shell as shown here, however that doesn't seem like the ideal approach:

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,找不到任何帮助我。

I also looked at the API and could not find anything to help me here.

推荐答案

Dart IO库附带了 HttpClient 这基本上是你正在寻找。但是,您应该使用 http Pub包。将它添加到您的依赖文件中:

Dart IO library comes with a HttpClient which is basically what you are looking for. However, you should probably use the http Pub package instead. Add it to your dependencies file:

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中的Curl类似的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆