[英]Poco c++Net: Http get headers from response
Im using POCO C++ Net libraries for http 我使用POCO C ++ Net库作为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
? 那么如何从
httpResponse
提取缓存头?
I've seen that you can do this in Java, ( getFirstHeader()
), but how do I do it in POCO C++? 我已经看到你可以用Java做这个(
getFirstHeader()
),但是我怎么用POCO C ++做呢?
Below is an ordinary http GET-request using POCO: 以下是使用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;
那么如何从httpResponse中提取缓存头?
res.get("Cache-control");
In the last try
block the following can be added to print out the headers to screen: 在最后一个
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.