如何从curlpp请求中检索响应cookie?
我想存储HTTP GET请求中的PHP会话。这是我当前的代码:
void Grooveshark::Connection::processPHPCookie()
{
std::ostringstream buffer;
gsDebug("Processing PHP cookie...");
try {
request.setOpt<cURLpp::Options::Url>("http://listen.grooveshark.com");
request.setOpt<cURLpp::Options::WriteStream>(&buffer);
request.perform();
// Get the PHP Session cookie here..
} catch (cURLpp::LogicError& exception) {
gsError(exception.what());
} catch (cURLpp::RuntimeError& exception) {
gsError(exception.what());
}
gsDebug("Processing complete...");
}request是一个cURLpp::Easy实例。如果你需要更多细节,你可以找到我的源代码here
提前谢谢。
发布于 2020-05-17 23:07:57
首先,设置exEasy.setOpt(curlpp::options::CookieFile(""),然后调用exEasy.perform(),然后遍历
std::list<std::string> cookies;
curlpp::infos::CookieList::get(exEasy, cookies);发布于 2013-03-26 13:03:50
https://bitbucket.org/moriarty/curlpp/src/ac658073c87a/examples/example07.cpp
这个例子似乎有你想要的东西。特别是这段代码:
std::cout << "\nCookies from cookie engine:" << std::endl;
std::list<std::string> cookies;
curlpp::infos::CookieList::get(exEasy, cookies);
int i = 1;
for (std::list<std::string>::const_iterator it = cookies.begin(); it != cookies.end(); ++it, i++)
{
std::cout << "[" << i << "]: " << MakeCookie(*it) << std::endl;
}请注意,MakeCookie在示例中返回一个名为MyCookie的结构,因此您还需要:
struct MyCookie
{
std::string name;
std::string value;
std::string domain;
std::string path;
time_t expires;
bool tail;
bool secure;
};
MyCookie
MakeCookie(const std::string &str_cookie)
{
std::vector<std::string> vC = splitCookieStr(str_cookie);
MyCookie cook;
cook.domain = vC[0];
cook.tail = vC[1] == "TRUE";
cook.path = vC[2];
cook.secure = vC[3] == "TRUE";
cook.expires = StrToInt(vC[4]);
cook.name = vC[5];
cook.value = vC[6];
return cook;
}发布于 2019-04-15 08:17:59
之前的答案链接现在位于:https://github.com/datacratic/curlpp/blob/master/examples/example07.cpp
应该注意的是,如果只想获得cookie响应,则必须向cookie列表传递一个空字符串。
对于前面的示例,需要添加exEasy.setOpt(new curlpp::options::CookieList(""))才能获得cookie字符串(可能会使用空字符串以外的其他内容,但我无法找到更多文档)。
https://stackoverflow.com/questions/4356911
复制相似问题