如何通过curlpp 检索响应cookie?

发布于 2024-10-05 16:24:50 字数 909 浏览 3 评论 0原文

如何从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 实例。如果您需要更多详细信息,可以在此处找到我的源代码< /a>

提前致谢。

how can I retrieve the response cookies from a curlpp request?

I want to store the PHP session off of a HTTP GET request. 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. If you need more details you can find my source code here

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

许你一世情深 2024-10-12 16:24:50

首先,设置 exEasy.setOpt(curlpp::options::CookieFile("")
然后调用exEasy.perform(),
然后循环遍历

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

First, set exEasy.setOpt(curlpp::options::CookieFile("")
then call exEasy.perform(),
then loop through

std::list<std::string> cookies;
curlpp::infos::CookieList::get(exEasy, cookies);
笑忘罢 2024-10-12 16:24: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;
}

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:

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

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

应该注意的是,如果只想获取 cookie 响应,则必须向 cookie 列表传递一个空字符串。

对于前面的示例,需要添加 exEasy.setOpt(new curpp::options::CookieList("")) 才能获取 cookie 字符串(可能会使用空字符串以外的其他内容,但我无法找到更多文档)。

The previous answers link is now located at: 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.

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文