Poco c++Net: Http get headers from response

╄→гoц情女王★ 提交于 2020-01-03 16:55:51

问题


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;

回答1:


So how can i extract the cache header from httpResponse?

res.get("Cache-control");



回答2:


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;
}


来源:https://stackoverflow.com/questions/15991496/poco-cnet-http-get-headers-from-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!