如何解决使用Poco C ++下载图像时出现问题
我正在尝试在C ++中通过Poco从URL链接下载图像,但是遇到了问题。每当运行下面的代码(receiveResponse部分)时,它总是捕获NoMessageException。下面的2个代码块来自: https://www.codeproject.com/articles/252566/learning-poco-get-with-http和C++ 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代码)。 谢谢。
解决方法
您的问题是您正在使用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;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。