且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

服务器端Dart中的跨网域请求

更新时间:2023-11-27 22:09:22

听起来你可能会问一个服务器端命令行脚本,它可以向HTTP服务器发出请求。虽然我的问题的措辞并不完全清楚。 (上面的答案是关于基于浏览器的Dart脚本。)

It sounds like you might be asking about writing a server side command line script which can make requests to an HTTP server. Though the wording of question isn't totally clear to me. (The answers above are about browser based Dart scripts.)

这是Dart的可能。

This is possible with Dart. There are no cross origin restrictions in this case.

请参阅 HttpClient 类。
或者您可以在pub上使用 http 包。

我推荐使用http包,因为它提供了一个更简单的高级接口。

I recommend using the http package, as it provides a simpler high-level interface.

这里是一个使用http包的例子: / p>

Here is an example using the http package:

import 'dart:io';
import 'package:http/http.dart' as http;

main() {
    http.read("http://google.com").then((content) {
        print(content);
    });
}


$ b $ p

您需要更新pubspec.yaml文件才能添加以下依赖项:

You'll need to update your pubspec.yaml file to add the following dependencies:

name: Http Example
   dependencies:
     http: any
     pathos: any

(实际上,你应该只需要包括http,但我认为http包缺少pathos依赖在它的pubspec.yaml文件。)

(Actually, you should only need to include http, but I think the http package is missing the pathos dependency in it's pubspec.yaml file.)

我找不到http的漂亮文档,但在源文件

I couldn't find the pretty documentation for http, but there is some doc comments in the source file.