2010-12-05 71 views
3

如何從curlpp請求中檢索響應cookie?如何通過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

在此先感謝。

回答

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