Poco c ++ Net:Http从响应中获取头文件

Dav*_*son 3 c++ http http-caching poco-libraries

我使用POCO C++ Net库作为http

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

首先,我想我需要从缓存标题中过期并使用缓存值进行交叉检查(请告诉我,如果我错了).

那么如何从中提取缓存头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;
Run Code Online (Sandbox Code Playgroud)

Ale*_*lex 6

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

res.get("Cache-control");
Run Code Online (Sandbox Code Playgroud)


Dav*_*son 5

在最后一个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;
}
Run Code Online (Sandbox Code Playgroud)