![](/img/trans.png)
[英]How to set content-type for request.response in Dart Server?
[英]Dart http server freezes when content type is set to text/html
我有一個非常簡單的 Dart HttpServer
,如下所示:
server.listen((request) async {
final html = await indexHtml; // Loaded from a file.
request.response.write(html);
await request.response.close();
});
效果很好(除了它將 HTML 顯示為文本),但是如果我像這樣正確設置內容類型:
server.listen((request) async {
final html = await indexHtml;
request.headers.set("Content-type", "text/html");
request.response.write(html);
await request.response.close();
});
然后它凍結並且永遠不會返回響應。 我試過遠程登錄它並輸入GET / HTTP/1.1
但它永遠不會返回任何數據。 甚至沒有標題。 Chrome 永遠旋轉。
我是在做一些愚蠢的事情還是這壞了?
呃,我很愚蠢,它確實在日志中給了我一個例外:
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: HttpException: HTTP headers are not mutable
我試圖修改請求標頭,而不是響應標頭。 代替
request.headers.set("Content-type", "text/html");
它應該是
request.response.headers.set("Content-type", "text/html");
更確切地說
request.response.headers.contentType = ContentType.html;
正如 julemand101 建議的那樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.