import 'dart:io';
import 'dart:async';
void main() {
HttpClient client = new HttpClient();
client.getUrl(Uri.parse('http://api.dartlang.org/docs/releases/latest/dart_io/HttpClientResponse.html'))
.then((HttpClientRequest request) => request.close())
.then((HttpClientResponse response) {
response.listen(print, onError: (e) {
print('error: $e');
});
});
}
上面的代码不起作用,使用类似的方法来监听pipe
并且fold
还抛出异常=>Breaking on exception: The null object does not have a method 'cancel'.
更新
这是连接到本地计算机时的代码示例。
import 'dart:io';
import 'dart:async';
void main() {
HttpServer.bind('127.0.0.1', 8080)
.then((HttpServer server) {
server.listen((HttpRequest request) {
File f = new File('upload.html');
f.openRead().pipe(request.response);
});
HttpClient client = new HttpClient();
client.getUrl(Uri.parse('http://127.0.0.1:8080'))
.then((HttpClientRequest request) => request.close())
.then((HttpClientResponse response) {
response.listen(print, onError: (e) {
print('error: $e');
});
});
});
}
它首先打印出字节然后抛出异常Breaking on exception: The null object does not have a method 'cancel'.
Dart 编辑器版本 0.7.2_r27268。Dart SDK 版本 0.7.2.1_r27268。在 Windows 64 位机器上。