使用Poco C ++库的HTTPRequestHandler返回CSV文件

时间:2018-04-14 19:33:18

标签: c++ http poco-libraries

我有一个使用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;
    }   
};

1 个答案:

答案 0 :(得分: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;
    }   
};