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

问题描述

我正在尝试在C ++中通过Poco从URL链接下载图像,但是遇到了问题。每当运行下面的代码(receiveResponse部分)时,它总是捕获NoMessageException。下面的2个代码块来自: https://www.codeproject.com/articles/252566/learning-poco-get-with-httpC++ Http Request with POCO

示例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,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格式不正确(尽管认为它没有任何问题)还是因为它实际上根本没有返回任何消息? (但是我用邮递员进行了测试,并且2 url确实给出了某种响应。例如,它将返回html代码)。 谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)