【问题标题】:How to read image content from an HTTP server response using Poco C++?如何使用 Poco C++ 从 HTTP 服务器响应中读取图像内容?
【发布时间】:2012-01-14 14:21:44
【问题描述】:

我正在使用 Poco 用 C++ 编写 HTTP 客户端,并且存在服务器发送带有 jpeg 图像内容(以字节为单位)的响应的情况。我需要客户端处理响应并从这些字节生成 jpg 图像文件。

我在 Poco 库中搜索了合适的函数,但没有找到。看来只能手动了。

这是我的代码的一部分。它接受响应并使输入流从图像内容的开头开始。

    /* Get response */
    HTTPResponse res;
    cout << res.getStatus() << " " << res.getReason() << endl;

    istream &is = session.receiveResponse(res);

    /* Download the image from the server */
    char *s = NULL;
    int length;
    std::string slength;

    for (;;) {
        is.getline(s, '\n');
        string line(s);

        if (line.find("Content-Length:") < 0)
            continue;

        slength = line.substr(15);
        slength = trim(slength);
        stringstream(slength) >> length;

        break;
    }

    /* Make `is` point to the beginning of the image content */
    is.getline(s, '\n');

如何进行?

【问题讨论】:

    标签: c++ image http client poco-libraries


    【解决方案1】:

    以下是将响应正文作为字符串获取的代码。您也可以使用 ofstream 将其直接写入文件(见下文)。

        #include <iostream>
        #include <sstream>
        #include <string>
    
        #include <Poco/Net/HTTPClientSession.h>
        #include <Poco/Net/HTTPRequest.h>
        #include <Poco/Net/HTTPResponse.h>
        #include <Poco/Net/Context.h>
        #include <Poco/Net/SSLManager.h>
        #include <Poco/StreamCopier.h>
        #include <Poco/Path.h>
        #include <Poco/URI.h>
        #include <Poco/Exception.h>
    
    
        ostringstream out_string_stream;
    
        // send request
        HTTPRequest request( HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1 );
        session.sendRequest( request );
    
        // get response
        HTTPResponse response;
        cout << response.getStatus() << " " << response.getReason() << endl;
    
        // print response
        istream &is = session.receiveResponse( response );
        StreamCopier::copyStream( is, out_string_stream );
    
        string response_body = out_string_stream.str();
    

    要直接写入文件,可以这样:

        // print response
        istream &is = session->receiveResponse( response );
    
        ofstream outfile;
        outfile.open( "myfile.jpg" );
    
        StreamCopier::copyStream( is, outfile );
    
        outfile.close();
    

    【讨论】:

    • 我必须用二进制写出才能正常工作:outfile.open("myfile.jpg", ios_base::binary);
    • @ryatkins 你在 Windows 上吗?
    • 是的,在 Windows 上。否则,我的图像下载会损坏。你认为这是 Windows 特有的吗?
    【解决方案2】:

    不要重新发明轮子。正确地执行 HTTP 是很困难的。使用现有的库,例如 libcurl。

    【讨论】:

    • 我不重新发明轮子,我使用 Poco。
    • Leif,如果 Poco 包含一个 HTTP 库,并且您会使用它,那么您就不必解析 Content-Length。
    • 如果您知道一种无需手动解析即可使用 Poco 查找 Content-Length 的方法,那您为什么不告诉我它是什么?
    • 因为我不认识 Poco。但是确定有效负载的长度是 HTTP 库为您所做的事情之一,否则它不是 HTTP 库(请注意,这可能意味着库不知道,您需要读取有效负载直到结束;当有效负载使用分块编码时会发生这种情况)。话虽这么说:pocoproject.org/docs/Poco.Net.HTTPMessage.html#13185
    • 朱利安,我同意你的观点,但这不应该是评论吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 2011-12-30
    相关资源
    最近更新 更多