2

我正在尝试使用 Ubuntu Linux 上的 POCO C++ 库从网络流中解压缩文件,但解压缩失败并出现“非法状态”异常。HTTPResponsestatus 和 reason 是302 Moved Temporarily。同时我可以用浏览器下载和解压链接。HTTPClientSessionHTTPResponse处于这种状态时我该怎么办?

  ...
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 ();
  ...
4

2 回答 2

2

你可以用 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是怎么做的。

于 2015-11-15T06:56:58.363 回答
1

302响应中,应该有一个标头字段指出新位置。您只需点击该链接即可。

请参阅此链接此 Wikipedia 页面,当然还有实际的HTTP 1.1 RFC

于 2013-08-02T07:13:52.457 回答