使用 Poco C++ 下载图像时出现问题

Problem downloading an image with Poco C++

我正在尝试通过 C++ 中的 Poco 从 url link 下载图像,但遇到了问题。只要下面的代码是 运行(receiveResponse 部分),它总是会捕获 NoMessageException。以下 2 个代码块引用自: https://www.codeproject.com/articles/252566/learning-poco-get-with-http and

示例 1

   try
        {
        URI uri("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png");
        std::string path(uri.getPathAndQuery());
        if (path.empty()) path = "/";
        HTTPClientSession session(uri.getHost(), uri.getPort());
        HTTPRequest request(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
        HTTPResponse response;

        bool isSuccess = false;
        session.sendRequest(request);
        std::istream& rs = session.receiveResponse(response);
        std::cout << response.getStatus() << " " << response.getReason() << std::endl;
        if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED)
        {
            std::ofstream ofs("Poco_banner.png",std::fstream::binary); 
            StreamCopier::copyStream(rs, ofs);
            isSuccess = true;
        }
        else
        {
            //it went wrong ?
            isSuccess = false;
        }

        if (!isSuccess)
        {
            std::cerr << "Invalid username or password" << std::endl;
            return;
        } else
        {
            cout << "done download" << endl;
        }
    }
    catch(Exception& exc)
    {
        std::cerr << exc.displayText() << std::endl;
    }

示例 2

   try
        {
            // prepare session
            URI uri("https://www.google.com");
            HTTPClientSession session(uri.getHost(), uri.getPort());

            // prepare path
            string path(uri.getPathAndQuery());
            if (path.empty()) path = "/";

            // send request
            HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
            session.sendRequest(req);

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

            // print response
            istream &is = session.receiveResponse(res);
            StreamCopier::copyStream(is, cout);
        }
        catch (Exception &ex)
        {
            cerr << ex.displayText() << endl;
            return;
        }

我包含的头文件

#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/HTTPCredentials.h"
#include "Poco/StreamCopier.h"
#include "Poco/NullStream.h"
#include "Poco/Path.h"
#include "Poco/URI.h"
#include "Poco/Exception.h"
#include <iostream>

using Poco::Net::HTTPClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPMessage;
using Poco::StreamCopier;
using Poco::Path;
using Poco::URI;
using Poco::Exception;

using namespace std;

我不确定代码有什么问题。是因为 url 格式不正确(虽然不要认为它有任何问题)还是因为它真的 return 根本没有任何消息? (但我用邮递员测试了它,2 url 确实给出了某种响应。例如,它将 return html 代码)。 谢谢。

您的问题是您使用的是 HTTPS URI。 您的代码适用于修改后的 HTTP URI: http://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png

如果您想使用 HTTPS,请使用下面的代码,链接到 PocoNetSSL 库。

#include <Poco/MD5Engine.h>
#include <Poco/DigestStream.h>
#include <Poco/Net/Context.h>
#include <Poco/Net/HTTPSClientSession.h>

void HttpsDownload() {
    try {
        Poco::URI uri("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png");
        std::string path(uri.getPathAndQuery());
        if (path.empty())
            path = "/";
        const Poco::Net::Context::Ptr context = new Poco::Net::Context(
                Poco::Net::Context::CLIENT_USE, "", "", "",
                Poco::Net::Context::VERIFY_NONE, 9, false,
                "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
        Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort(), context);
        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, path, Poco::Net::HTTPMessage::HTTP_1_1);
        Poco::Net::HTTPResponse response;

        bool isSuccess = false;
        session.sendRequest(request);
        std::istream &rs = session.receiveResponse(response);
        std::cout << response.getStatus() << " " << response.getReason() << std::endl;
        if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED) {
            std::ofstream ofs("Poco_banner.png", std::fstream::binary);
            Poco::StreamCopier::copyStream(rs, ofs);
            isSuccess = true;
        } else {
            //it went wrong ?
            isSuccess = false;
        }

        if (!isSuccess) {
            std::cerr << "Invalid username or password" << std::endl;
            return;
        } else {
            std::cout << "done download" << std::endl;
        }
    } catch (Exception &exc) {
        std::cerr << exc.displayText() << std::endl;
    }
}