4

我为 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;
4

2 回答 2

8

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

res.get("Cache-control");
于 2013-04-13T23:15:02.650 回答
3

在最后一个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;
}
于 2013-04-13T19:13:12.773 回答