import 'package:http/http.dart' as http;
main(List<String> args) async {
var url = Uri.http('localhost:5000', '/test');
var r = await http.get(url);
print(r.body); // hello world
print(r.statusCode); // 服务器返回的状态码200
}
@Get('test')
getHello(): string {
return 'hello wolrd';
}
import 'package:http/http.dart' as http;
main(List<String> args) async {
var url = Uri.http('localhost:5000', '/test');
var r = await http.get(url);
print(r.body); // {"statusCode":500,"error":"Internal Server Error","message":"服务器错误"}
print(r.statusCode); // 500
}
@Get('test')
getHello(): string {
throw new InternalServerErrorException('服务器错误');
return 'hello wolrd';
}
import 'package:http/http.dart' as http;
main(List<String> args) async {
var client = http.Client();
Future.delayed(Duration(seconds: 5)).then((_) {
print('关闭这个请求');
client.close();
});
try {
var url = Uri.http('localhost:5000', '/test');
print('请求开始');
var r = await client.get(url);
print(r.body);
print(r.statusCode);
} on http.ClientException catch(e) {
/// 捕获中断后抛出的错误
print(e);
} catch (e) {
print('Other Error: $e');
}
}
@Get('test')
async getHello(): Promise<string> {
//延迟 10s后发出数据
return await new Promise(res => {
setTimeout(() => {
res('hello world');
}, 10000);
});
}
打印结果
$ dart ./bin/main.dart
请求开始
关闭这个请求
Connection closed before full header was received
原文:https://www.cnblogs.com/ajanuw/p/10999826.html