从http头中提取cookie 我正在编写一个C++函数,它将从HTTP报头中提取cookie。标题位于字符串中,如下所示: HTTP/1.1 200 OK cache-control: no-cache, no-store, max-age=0, must-revalidate content-language: en content-length: 3202 content-type: text/html; charset=utf-8 date: Fri, 25 Apr 2014 13:31:44 GMT etag: "46ec0cd3920851f7b63dbaa70280cd32" expires: Mon, 01 Jan 1990 00:00:00 GMT pragma: no-cache server: tfe set-cookie: d=32; path=/; expires=Sat, 25-Apr-2015 13:31:44 GMT set-cookie: req_country=United+Kingdom; path=/; expires=Sun, 25-May-2014 13:31:44 GMT

从http头中提取cookie 我正在编写一个C++函数,它将从HTTP报头中提取cookie。标题位于字符串中,如下所示: HTTP/1.1 200 OK cache-control: no-cache, no-store, max-age=0, must-revalidate content-language: en content-length: 3202 content-type: text/html; charset=utf-8 date: Fri, 25 Apr 2014 13:31:44 GMT etag: "46ec0cd3920851f7b63dbaa70280cd32" expires: Mon, 01 Jan 1990 00:00:00 GMT pragma: no-cache server: tfe set-cookie: d=32; path=/; expires=Sat, 25-Apr-2015 13:31:44 GMT set-cookie: req_country=United+Kingdom; path=/; expires=Sun, 25-May-2014 13:31:44 GMT,c++,string,C++,String,我需要此函数来查找cookies: set-cookie: d=32; path=/; expires=Sat, 25-Apr-2015 13:31:44 GMT set-cookie: req_country=United+Kingdom; path=/; expires=Sun, 25-May-2014 13:31:44 GMT 把它们放在另一根绳子上,看起来是这样的: d=32; req_country=United+Kingdom; 每个标头中也可以有2个以上的cookie 我试过:

我需要此函数来查找cookies:

set-cookie: d=32; path=/; expires=Sat, 25-Apr-2015 13:31:44 GMT
set-cookie: req_country=United+Kingdom; path=/; expires=Sun, 25-May-2014 13:31:44 GMT
把它们放在另一根绳子上,看起来是这样的:

d=32; req_country=United+Kingdom;
每个标头中也可以有2个以上的cookie

我试过:

size_t p1 = header_data.find("set-cookie:");
size_t p2 = header_data.find(";");

std::string head = header_data.substr(p1,p2-p1);
执行之后,它给了我以下错误:

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr
Aborted (core dumped)

试试这个代码。未优化,但我猜它以您想要的方式工作:

#include <iostream>
#include <vector>
#include <sstream>

using namespace std;

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}


std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    split(s, delim, elems);
    return elems;
}

int main() {
    std::string header =
            "HTTP/1.1 200 OK\n"
            "cache-control: no-cache, no-store, max-age=0, must-revalidate\n"
            "content-language: en\n"
            "content-length: 3202\n"
            "content-type: text/html; charset=utf-8\n"
            "date: Fri, 25 Apr 2014 13:31:44 GMT\n"
            "etag: \"46ec0cd3920851f7b63dbaa70280cd32\"\n"
            "expires: Mon, 01 Jan 1990 00:00:00 GMT\n"
            "pragma: no-cache\n"
            "server: tfe\n"
            "set-cookie: d=32; path=/; expires=Sat, 25-Apr-2015 13:31:44 GMT\n"
            "set-cookie: req_country=United+Kingdom; path=/; expires=Sun, 25-May-2014 13:31:44 GMT\";\n";

    vector<string> headerLines = split(header, '\n');

    for (int i(0); i != headerLines.size(); ++i) {
        if (headerLines[i].find("set-cookie:") != std::string::npos) {
            std::string variablesPart = split(split(headerLines[i], ';')[0], ':')[1];
            std::cout << "\nExtracted: {" << variablesPart << "}";
        }
    }
}
#包括
#包括
#包括
使用名称空间std;
std::vector&split(常量std::string&s、char-delim、std::vector&elems){
标准::stringstream ss(s);
std::字符串项;
while(std::getline(ss,item,delim)){
元素推回(项目);
}
返回元素;
}
std::vector split(const std::string&s,char delim){
std::向量元素;
拆分(s、delim、elems);
返回元素;
}
int main(){
字符串头=
“HTTP/1.1 200正常\n”
缓存控制:无缓存,无存储,最大使用期限=0,必须重新验证\n
“内容语言:en\n”
“内容长度:3202\n”
“内容类型:text/html;字符集=utf-8\n”
日期:2014年4月25日星期五13:31:44 GMT\n
“etag:\“46ec0cd3920851f7b63dbaa70280cd32\”\n”
过期时间:1990年1月1日星期一00:00:00 GMT\n
“pragma:没有缓存\n”
“服务器:tfe\n”
“设置cookie:d=32;path=/;expires=Sat,2015年4月25日13:31:44 GMT\n”
“设置cookie:req_country=United+Kingdom;path=/;expires=Sun,2014年5月25日13:31:44 GMT\”;\n”;
向量头线=拆分(头,'\n');
for(int i(0);i!=headerLines.size();+i){
if(headerLines[i].find(“set cookie:)!=std::string::npos){
std::string variablespat=split(split(headerLines[i],';')[0],':')[1];

std::我可以尝试
size\utp1=header\u data.find(“设置cookie:”);size\utp2=header\u data.find(;”);std::string head=header\u data.substr(p1,p2-p1);
它给了我一个错误。首先,请编辑您的问题以包含代码。其次,如果您遇到编译器错误,请编辑问题以包含完整且未编辑的错误日志,并显示错误在代码中的位置。如果您遇到的错误不是编译器错误,请详细说明(例如,显示程序的实际输出与预期输出)。至于您的问题,您在注释中显示的对
find
的第二次调用将从字符串
header\u data
的开头开始。您可能需要阅读,例如。您知道http头是由
“\r\n”
分隔的,而不是简单的
“\n”