C++Curl添加和发送cookie
C++ Curl adding and sending cookies
我一直在尝试编写一个发送post数据和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引擎",这是用完成的
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
使用CURLOPT_COOKIE:设置COOKIE,而不是显式设置HTTP标头
curl_easy_setopt(curl, CURLOPT_COOKIE, "name=xxx; name2=xxx;");
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 只能向C++添加一定数量的字符
- Qt和C++:将QLineEdit添加到QTabWidget中
- 将QIcon添加到QTableView单元格
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- 将图像添加到资源文件夹UWP C++
- C++Curl添加和发送cookie