我尝试在 Ubuntu Linux 上使用 POCO C++ 库从网络流中解压缩文件,但解压失败并出现“非法状态”异常。 HTTPResponse
状态和原因为 302 暂时移动。同时我可以用浏览器下载并解压链接。当 HTTPResponse
处于这种状态时,我应该如何处理 HTTPClientSession
?
...
HTTPResponse res;
std::istream& rs = h_ses.receiveResponse (res);
if (res.getStatus () != HTTPResponse::HTTP_OK) {
poco_error (logger, "http response status: " + std::to_string (res.getStatus ()) + " " + res.getReason ());
}
if (!rs) {
poco_error (logger, "responese stream is in bad state: " + std::to_string (rs.rdstate()));
}
Decompress dec (rs, target_dir_.native ());
poco_debug (logger, "Unzipping: " + dl + " ...");
dec.EError += Poco::Delegate<Addon_Loader, std::pair<const Poco::Zip::ZipLocalFileHeader, const std::string> >(this, &Addon_Loader::on_decompress_error);
dec.decompressAllFiles ();
...
最佳答案
您可以使用 HTTPStreamFactory 来做到这一点;这是一个完整的示例:
#include "Poco/URI.h"
#include "Poco/URIStreamOpener.h"
#include "Poco/Net/HTTPStreamFactory.h"
#include "Poco/StreamCopier.h"
#include <iostream>
#include <memory>
using namespace Poco;
using namespace Poco::Net;
int main()
{
URIStreamOpener opener;
opener.registerStreamFactory("http", new HTTPStreamFactory);
URI uri("http://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com%2F");
std::auto_ptr<std::istream> pStr(opener.open(uri));
StreamCopier::copyStream(*pStr.get(), std::cout);
return 0;
}
如果你必须使用HTTPClientSession,看看如何HTTPStreamFactory做到了。
关于c++ - POCO C++ 库,HTTPResponse 状态错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18010855/