POCO C++库,HTTPResponse状态错误

POCO C++ Library, HTTPResponse bad status

本文关键字:状态 错误 HTTPResponse C++ POCO      更新时间:2023-10-16

我试图在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是如何做到的。

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

例如,请参阅此链接或维基百科页面,当然还有实际的HTTP 1.1 RFC。