tgw_l7_route=d0bf4a9ab78d53762b596c0a48dabcdf; Expires=Thu, 03-May-2018 11:42:51 GMT; Path=/, session=a1d25e28-0084-421d-ae71-9ae18c7f6b50; Expires=Sun, 03-Jun-2018 10:42:51 GMT; HttpOnly; Path=/
有 2 个 Set-Cookie 项由 ', ' 连接,这个字符串的问题是过期日期也包含 ', '。
该字符串由 cpprestsdk 库生成。我需要解析它并生成一个“Cookie”标头,以便在正在进行的请求中发送到服务器。
// Example program
#include <iostream>
#include <string>
#include <regex>
#include <iterator>
int main()
{
std::string cookieStr = "tgw_l7_route=d0bf4a9ab78d53762b596c0a48dabcdf; Expires=Thu, 03-May-2018 11:42:51 GMT; Path=/, session=a1d25e28-0084-421d-ae71-9ae18c7f6b50; Expires=Sun, 03-Jun-2018 10:42:51 GMT; HttpOnly; Path=/";
std::regex rgx(", [^ ]+=");
std::sregex_token_iterator iter(cookieStr.begin(),
cookieStr.end(),
rgx,
-1);
std::sregex_token_iterator end;
for ( ; iter != end; ++iter)
std::cout << *iter << '\n';
}
以上代码输出:
tgw_l7_route=d0bf4a9ab78d53762b596c0a48dabcdf; Expires=Thu, 03-May-2018 11:42:51 GMT; Path=/
a1d25e28-0084-421d-ae71-9ae18c7f6b50; Expires=Sun, 03-Jun-2018 10:42:51 GMT; HttpOnly; Path=/
有没有办法在第二个字符串中保留“session=”?