繁体   English   中英

vibed:在输出流的末尾写入

[英]vibed: Writing past end of output stream

我不明白为什么会出错:在下一个代码上:

void logout(HTTPServerRequest req, HTTPServerResponse res)
{
    try
    {
        logInfo("Logout section");
        Json request = req.json;
        Json responseBody = Json.emptyObject; // 

        if (req.session) // if user have active session
        {
            res.terminateSession();
            responseBody["status"] = "success";
            responseBody["isAuthorized"] = false;
            res.writeJsonBody(responseBody);
            logInfo("-------------------------------------------------------------------------------");
            logInfo(responseBody.toString);
            logInfo("^-----------------------------------------------------------------------------^");                              
            logInfo("User %s logout", request["username"]); //
            logInfo("User 12333333333333 logout"); //
        }

        else
        {
            responseBody["status"] = "fail"; // user do not have active session?
            logInfo("User do not have active session"); 
            res.writeJsonBody(responseBody);
        }
    writeln("-------before-------");
    writeln(responseBody.toString);
    res.writeJsonBody(responseBody);
    writeln("-------after-------");
    }

    catch (Exception e)
    {
        logInfo(e.msg);
        writeln("3333");
    }
}

这是截图

我做错了什么?

writeJsonBody序列化响应JSON 一次 ,设置statuscontent_type和也关闭输出流。 仔细看看您的代码-它两次调用res.writeJsonBody(responseBody)

如果要流式传输响应,则可以像这样res.bodyWriter.put("a sentence.")一样访问输出流,但是请注意,第一次访问该流后,不允许更改任何内容。响应的标头(例如状态代码),因为标头已发送到客户端。

顺便说一句,您可能对Vibed的高级REST API功能感兴趣。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM