【问题标题】:Return CSV file with Poco C++ library's HTTPRequestHandler使用 Poco C++ 库的 HTTPRequestHandler 返回 CSV 文件
【发布时间】:2018-04-14 19:33:18
【问题描述】:

我有一个使用 Poco C++ 库来处理 HTTP 请求的服务器应用程序。我真的找不到太多关于如何从 HTTP 服务器返回 CSV 文件的文档或示例。有人做过吗?

本质上这是我到目前为止所拥有的,但我不明白如何将文本作为 CSV 文件返回。

/// Return a CSV document.
class GetCSVFileHandler: public HTTPRequestHandler
{
public:
    GetCSVFileHandler()
    {          
    }

    void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
    {        
        std::string result = "1,2,3\n a,b,c\n";

        // ??????????????????????????????????????????????????
        // how to return a CSV file here?
        // ??????????????????????????????????????????????????
        std::ostream& ostr = response.send();
        ostr << result;
    }   
};

【问题讨论】:

  • 您能否详细说明一下:究竟是什么问题?

标签: c++ http poco-libraries


【解决方案1】:

试试这个:

// Return a CSV document.
class GetCSVFileHandler: public HTTPRequestHandler
{
public:

    void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
    {

        response.setChunkedTransferEncoding(true);
        response.setContentType("text/csv");

        std::string result = "1,2,3\n a,b,c\n";
        std::ostream& ostr = response.send();
        ostr << result;
    }   
};

【讨论】:

    猜你喜欢
    • 2013-03-24
    • 1970-01-01
    • 2023-02-12
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多