如何通过curlpp检索响应cookie?

时间:2010-12-05 01:38:23

标签: c++ curlpp

如何从curlpp请求中检索响应cookie?

我希望将PHP会话存储在HTTP GET请求之外。这是我目前的代码:

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...");
}

requestcURLpp::Easy个实例。如果您需要更多详细信息,可以找到我的源代码here

提前致谢。

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

以前的答案链接现在位于:https://github.com/datacratic/curlpp/blob/master/examples/example07.cpp

应该注意,如果只想获取Cookie响应,则必须将一个空字符串传递给Cookie列表。

对于前面的示例,需要添加exEasy.setOpt(new curlpp::options::CookieList(""))才能获得Cookie字符串(可能使用了空字符串以外的其他字符,但我找不到更多文档)。

答案 2 :(得分:0)

首先,设置exEasy.setOpt(curlpp::options::CookieFile("") 然后致电exEasy.perform(), 然后遍历

std::list<std::string> cookies;
curlpp::infos::CookieList::get(exEasy, cookies);