【问题标题】:Poco c++Net: Http get headers from responsePoco c++Net:Http 从响应中获取标头
【发布时间】:2013-04-06 04:03:57
【问题描述】:

我正在为 http 使用 POCO C++ Net 库

我想尝试为持久缓存制定策略。

首先,我认为我需要从缓存标头中获取过期时间并与缓存值进行交叉检查(如果我错了,请告诉我)。

那么如何从httpResponse 中提取缓存标头?

我已经看到您可以在 Java 中执行此操作 (getFirstHeader()),但是如何在 POCO C++ 中执行此操作?

下面是一个普通的使用 POCO 的 http GET 请求:

try
{
    // prepare session
    URI uri("http://www.google.se");
    HTTPClientSession session(uri.getHost(), uri.getPort());

    // prepare path
    string path(uri.getPathAndQuery());
    if (path.empty()) path = "/";

    // send request
    printnet(path.c_str());
    HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
    session.sendRequest(req);

    // get response
    printnet("Get response");

    HTTPResponse res;
    cout << res.getStatus() << " " << res.getReason() << endl;

    // print response
    istream &is = session.receiveResponse(res);
    StreamCopier::copyStream(is, cout);
}
catch (Exception &ex)
{
    printnet(ex.displayText().c_str());
    return -1;
}
return 0;

【问题讨论】:

    标签: c++ http http-caching poco-libraries


    【解决方案1】:

    那么如何从 httpResponse 中提取缓存头呢?

    res.get("Cache-control");
    

    【讨论】:

      【解决方案2】:

      在最后一个try 块中,可以添加以下内容以将标题打印到屏幕:

      cout << "RESPONSE:";
      string name;
      string value;
      NameValueCollection::ConstIterator i = res.begin();
      while(i!=res.end()){
      
          name=i->first;
          value=i->second;
          cout << name << "=" << value << endl << flush;
          ++i;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-06
        • 2023-01-12
        • 1970-01-01
        • 2017-07-28
        • 2019-03-04
        • 1970-01-01
        • 2013-05-28
        相关资源
        最近更新 更多