我有以下curl函数,它在循环中执行:
24/03/2017 01:43 PM <DIR> .
24/03/2017 01:43 PM <DIR> ..
24/03/2017 01:43 PM 195 CHANGELOG.md
24/03/2017 01:43 PM <DIR> css
24/03/2017 01:43 PM <DIR> dist
24/03/2017 01:43 PM <DIR> docs
24/03/2017 01:43 PM <DIR> lib
24/03/2017 01:43 PM 1,091 LICENSE.md
24/03/2017 01:43 PM 4,416 package.json
24/03/2017 01:43 PM 3,262 README.md
当我删除该行时,一切正常:
curl = curl_easy_init();
if (curl) {
CurlResponse = "";
host = "http://exaple.com";
LibcurlHeaders = curl_slist_append(LibcurlHeaders, "Expect:");
curl_easy_setopt(curl, CURLOPT_URL, (host).c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER , 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST , 1);
curl_easy_setopt(curl, CURLOPT_CAINFO, SSLPath.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, LibcurlHeaders);
curl_easy_setopt(curl, CURLOPT_VERBOSE, CurlVerbose);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, CurlPostData.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, LibcurlResponse);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &CurlResponse);
res = curl_easy_perform(curl);
curl_slist_free_all(LibcurlHeaders); <----------
if (res != CURLE_OK) {
LibcurlError(curl_easy_strerror(res), host);
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
然而,在Libcurl Docs中,它显示出像我一样使用它。至少这就是我对它的理解......
那么我做错了什么和/或错过了什么?
感谢您的回答
*编辑*
所以,基本上:
curl_slist_free_all(LibcurlHeaders);
我想要的是curl调用中使用的标头在curl调用完成后被清除,所以我得到: LibcurlHeaders = null:
LibcurlHeaders = null:
Curl call with headers
// cant clear headers
Curl call with 2 headers
// can't clear headers
Curl call with 3 headers
// can't clear headers
答案 0 :(得分:1)
你在评论中说你在循环中调用curl。你的例子没有显示出来。但假设您显示的代码是在实际处于循环中的较大代码内部,您只需要确保LibcurlHeaders
变量为NULL
,然后再首次调用curl_slist_append()
每个新的HTTP请求,例如:
curl = curl_easy_init();
if (curl) {
CurlResponse = "";
host = "http://exaple.com";
LibcurlHeaders = NULL; // <-- HERE
LibcurlHeaders = curl_slist_append(LibcurlHeaders, "Expect:");
/* alternatively, and ultimately safer and more accurate:
LibcurlHeaders = curl_slist_append(NULL, "Expect:");
*/
curl_easy_setopt(curl, CURLOPT_URL, host.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_easy_setopt(curl, CURLOPT_CAINFO, SSLPath.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, LibcurlHeaders);
curl_easy_setopt(curl, CURLOPT_VERBOSE, CurlVerbose);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, CurlPostData.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, LibcurlResponse);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &CurlResponse);
res = curl_easy_perform(curl);
curl_slist_free_all(LibcurlHeaders);
LibcurlHeaders = NULL; // <-- FOR GOOD MEASURE!
if (res != CURLE_OK) {
LibcurlError(curl_easy_strerror(res), host);
}
curl_easy_cleanup(curl);
}
答案 1 :(得分:0)
Wauw,
我成功解决了这个问题。它似乎是Libcurl的一个错误,可以很容易地修复。
问题是我在全球宣布struct curl_slist *LibcurlHeaders=NULL;
。
当我在每次struct curl_slist *LibcurlHeaders=NULL;
调用之前声明curl_easy_init();
并删除了全局声明时,curl_slist_free_all(LibcurlHeaders);
工作了,并且不再使我的程序崩溃!
<强> 短: 强>
问题是struct curl_slist *LibcurlHeaders=NULL;
的全局声明必须在每个curl_easy_init();
函数前面声明。