如何使用控制台dart应用程序执行HTTP POST(使用dart:io或可能是package:http库.我这样做:
import 'package:http/http.dart' as http;
import 'dart:io';
http.post(
url,
headers: {HttpHeaders.CONTENT_TYPE: "application/json"},
body: {"foo": "bar"})
.then((response) {
print("Response status: ${response.statusCode}");
print("Response body: ${response.body}");
}).catchError((err) {
print(err);
});
Run Code Online (Sandbox Code Playgroud)
但是得到以下错误:
Bad state: Cannot set the body fields of a Request with content-type "application/json".
Run Code Online (Sandbox Code Playgroud) 我试图在Dart中编写一个可以并行处理多个请求的HTTP服务器.到目前为止,我没有成功实现"并行"部分.
这是我最初尝试的内容:
import 'dart:io';
main() {
HttpServer.bind(InternetAddress.ANY_IP_V4, 8080).then((HttpServer server) {
server.listen((HttpRequest request) {
Stopwatch stopwatch = new Stopwatch();
stopwatch.start();
while (stopwatch.elapsedMilliseconds < 1000) { /* do nothing */ }
request.response.statusCode = HttpStatus.OK;
request.response.write(stopwatch.elapsedMilliseconds.toString());
request.response.close().catchError(print);
});
});
}
Run Code Online (Sandbox Code Playgroud)
在每个请求上它忙于工作一秒钟,然后完成.我让它以这种方式处理请求,以便它的时间可以预测,因此我可以很容易地看到Windows任务管理器中的请求的效果(CPU核心跳到100%使用率).
我可以说这不是并行处理请求,因为:
如果我将几个浏览器选项卡加载到http://example:8080/然后全部刷新,则选项卡依次加载,每个选项卡之间大约1秒钟.
如果我使用带有这些设置 的负载测试工具wrkwrk -d 10 -c 8 -t 8 http://example:8080/ ......它在我给它的10秒内完成了5到8个请求.如果服务器使用我的所有8个核心,我预计一个数字接近80个请求.
当我在wrk测试期间打开Windows任务管理器时,我发现我的核心中只有一个接近100%的使用率,其余的几乎都是空闲的.
所以,然后我尝试使用隔离,希望为每个请求手动生成一个新的隔离/线程:
import 'dart:io';
import 'dart:isolate';
main() {
HttpServer.bind(InternetAddress.ANY_IP_V4, 8080).then((HttpServer server) {
server.listen((HttpRequest request) {
spawnFunction(handleRequest).send(request);
});
});
}
handleRequest() {
port.receive((HttpRequest request, SendPort sender) { …Run Code Online (Sandbox Code Playgroud) 在我的.bashrc文件中:
export DART_SDK=/home/nicolas/dart/dart-sdk
Run Code Online (Sandbox Code Playgroud)
在命令行中,它在我"回显"它时起作用.但我无法看到这个用户变量dart,我只看系统变量而不是我的:
var env = Platform.environment;
env.forEach((k,v) => print("Key=$k Value=$v"));
Run Code Online (Sandbox Code Playgroud)
我试过了:
我的用户变量定义不明确吗?我的代码不好吗?这是一个错误?
我想创建一个文件,说foo/bar/baz/bleh.html,但没有目录foo,foo/bar/等等存在.
如何创建我的文件以递归方式创建所有目录?
我正在努力如何websockets在颤振中实现自动重新连接。我使用web_socket_channel,但是,该插件只是包装了dart.io WebSocket,因此任何基于WebSocket类的解决方案也适用于我。
我已经想通了,如何捕获套接字断开连接,请参阅下面的代码片段:
try {
_channel = IOWebSocketChannel.connect(
wsUrl,
);
///
/// Start listening to new notifications / messages
///
_channel.stream.listen(
_onMessageFromServer,
onDone: () {
debugPrint('ws channel closed');
},
onError: (error) {
debugPrint('ws error $error');
},
);
} catch (e) {
///
/// General error handling
/// TODO handle connection failure
///
debugPrint('Connection exception $e');
}
Run Code Online (Sandbox Code Playgroud)
我想IOWebSocketChannel.connect从inside 调用onDone,但是,这会导致一种无限循环 - 因为我必须再次关闭_channel先前的调用connect,这又会onDone再次调用等等。
任何帮助将不胜感激!
中的文件dart:io有许多同步和异步操作:
file.deleteSync()和file.delete()file.readAsStringSync()和file.readAsString()file.writeAsBytesSync(bytes)和file.writeAsBytes(bytes)在同步和异步选项之间进行选择时应牢记哪些注意事项?我似乎记得在某处看到如果您无论如何都必须等待同步选项完成(await file.delete()例如),同步选项会更快。但我不记得我在哪里看到过这句话,也不记得这是否属实。
这个方法有什么区别吗:
Future deleteFile(File file) async {
await file.delete();
print('deleted');
}
Run Code Online (Sandbox Code Playgroud)
这个方法:
Future deleteFile(File file) async {
file.deleteSync();
print('deleted');
}
Run Code Online (Sandbox Code Playgroud)