代码之家  ›  专栏  ›  技术社区  ›  Timmmm

当内容类型设置为text/html时,Dart http服务器冻结

  •  0
  • Timmmm  · 技术社区  · 5 年前

    我有一个非常简单的飞镖 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永远旋转。

    我是在做傻事,还是这个坏了?

    0 回复  |  直到 5 年前
        1
  •  1
  •   Timmmm    5 年前

    啊,我真是太愚蠢了,它确实在日志中给了我一个例外:

    [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所建议的那样。

    推荐文章