【发布时间】:2012-02-13 05:19:40
【问题描述】:
我一直在尝试编写一个发送帖子数据和 COOKIES 的程序。 Cookie添加部分似乎没有正确添加cookie...
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
int main(int argc, char *argv[])
{
CURL *curl;
CURLcode res;
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
static const char buf[] = "Expect:";
curl_global_init(CURL_GLOBAL_ALL);
/* Fill in the nick field */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "nick",
CURLFORM_COPYCONTENTS, "nichnameofxxx",
CURLFORM_END);
/* Fill in the pass field */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "pass",
CURLFORM_COPYCONTENTS, "passwordofxxx",
CURLFORM_END);
/* Other fields like ID */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "ProfileID",
CURLFORM_COPYCONTENTS, "77820",
CURLFORM_END);
/* Fill in the submit field too, even if this is rarely needed */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "submit",
CURLFORM_COPYCONTENTS, "send",
CURLFORM_END);
curl = curl_easy_init();
/* initalize custom header list (stating that Expect: 100-continue is not
wanted */
headerlist = curl_slist_append(headerlist, buf);
//Cookies here... it's a part of the Header
headerlist = curl_slist_append(headerlist, "Cookie: name=xxx; name2=xxx");
if(curl)
{
// what URL that receives this POST
curl_easy_setopt(curl, CURLOPT_URL, "http://www.mything.org/index.php?id=log2");
if ((argc == 2) && (!strcmp(argv[1], "noexpectheader")))
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_formfree(formpost);
curl_slist_free_all (headerlist);
}
return 0;
}
欢迎任何帮助!
【问题讨论】:
-
没有正确添加 cookie,这是什么意思?发生什么了?构建时是否有错误?运行时出错?您是否在调试器中单步执行了代码以查看发生了什么?
-
我有一个测试网站,如果你有正确的 COOKIE,你只能更改主题。我测试了该网站,它可以很好地捕获 cookie。所以唯一错误的是程序代码......
-
为什么您认为您的浏览器会将 cookie 保存到 libcurl 所做的相同位置?