Я пытаюсь загрузить изображение по URL-ссылке через Poco на C++, но столкнулся с проблемой. Он всегда перехватывает исключение NoMessageException всякий раз, когда запускается приведенный ниже код (часть receiveResponse). На приведенные ниже 2 блока кода ссылаются: https://www.codeproject.com/articles/252566/learning-poco-get-with-http и Запрос Http C++ с 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, 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-адреса неверен (хотя не думаю, что у него есть какие-то проблемы) или это потому, что он действительно вообще не возвращал никаких сообщений? (но я протестировал его с почтальоном, и 2 URL-адреса дали какой-то ответ. Например, он вернет html-коды). Спасибо.