Poco c++Net: Http get headers from response
Asked Answered
E

2

5

Im using POCO C++ Net libraries for http

I want to try make a strategy for persistent caching.

First of all I think I need to get expires from the cache headers and cross check with cached values (please tell me if I'm wrong).

So how can I extract the cache header from httpResponse?

I've seen that you can do this in Java, (getFirstHeader()), but how do I do it in POCO C++?

Below is an ordinary http GET-request using POCO:

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;
Entrepreneur answered 13/4, 2013 at 18:42 Comment(0)
K
9

So how can i extract the cache header from httpResponse?

res.get("Cache-control");
Karttikeya answered 13/4, 2013 at 23:15 Comment(0)
E
5

In the last try block the following can be added to print out the headers to screen:

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;
}
Entrepreneur answered 13/4, 2013 at 19:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.