简体   繁体   English

如何通过 curlpp 检索响应 cookie?

[英]How do I retrieve the response cookies through curlpp?

how can I retrieve the response cookies from a curlpp request?如何从 curlpp 请求中检索响应 cookie?

I want to store the PHP session off of a HTTP GET request.我想从 HTTP GET 请求中存储 PHP 会话。 This is my current code:这是我当前的代码:

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 is a cURLpp::Easy instance. request是一个cURLpp::Easy实例。 If you need more details you can find my source code here如果您需要更多详细信息,可以在此处找到我的源代码

Thanks in advance.提前致谢。

First, set exEasy.setOpt(curlpp::options::CookieFile("") then call exEasy.perform() , then loop through首先,设置exEasy.setOpt(curlpp::options::CookieFile("")然后调用exEasy.perform() ,然后循环

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

https://bitbucket.org/moriarty/curlpp/src/ac658073c87a/examples/example07.cpp https://bitbucket.org/moriarty/curlpp/src/ac658073c87a/examples/example07.cpp

That example seems to have what you want.那个例子似乎有你想要的。 In particular this code:特别是这段代码:

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

Note that MakeCookie returns a struct called MyCookie inside the example, so you would also need:请注意, 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;
}

The previous answers link is now located at: https://github.com/datacratic/curlpp/blob/master/examples/example07.cpp以前的答案链接现在位于: https ://github.com/datacratic/curlpp/blob/master/examples/example07.cpp

It should be noted that if one wants to just get cookie responses then on must pass an empty string to the cookie lists.应该注意的是,如果只想获得 cookie 响应,则必须将空字符串传递给 cookie 列表。

For the previous example exEasy.setOpt(new curlpp::options::CookieList("")) would need to be added in order to get cookie strings (something other than an empty string might be used but I was not able to find further documentation).对于前面的示例,需要添加exEasy.setOpt(new curlpp::options::CookieList(""))以获取 cookie 字符串(可能会使用除空字符串以外的其他内容,但我无法找到更多信息文档)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM