Cookies不会保存在cURL C ++中,这样的简单干净的代码示例将无法正常工作 [英] Cookies not being saved in cURL C++, such a simple and clean code example won't work

查看:85
本文介绍了Cookies不会保存在cURL C ++中,这样的简单干净的代码示例将无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不存储cookie?我创建的本地网站具有setcookie("test",time());在PHP中,它通常可以在浏览器中工作,但是在cURL中,它将不起作用,我什至不知道要尝试什么,这是我通过修剪代码获得的最简单的方法,但是仍然不保存cookie在一个文件中.它甚至不会创建文件来保存它(顺便说一句,由于我一直在移动并创建文件,因此Windows有权在根目录中添加任何文件),是的,该站点示例设置了cookie.怎么了?

why isn't the cookies storing? the local website I made has a setcookie("test",time()); in PHP and it works in the browser normally, but in cURL it just won't work, I don't even know what to try anymore, this is the simplest I got by trimming the code, and still don't save the cookies in a file. it doesn't even create the file to save it (btw the windows has the permissions to add any file in the root since I'm moving and creating files all the time there), and Yes the site example set cookies. what is wrong with it ?

内容输出正确,应该可以正常工作.但这是行不通的. :/

The content output is correct and it should work. but it just doesn't work. :/

提前谢谢.

#include <curl/curl.h>
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
            ((std::string*)userp)->append((char*)contents, size * nmemb);
            return size * nmemb;
        }

int main () {
        CURL *curl2;
        CURLcode res;




        curl_global_init(CURL_GLOBAL_ALL);



       string url2 = "http://localhost/test.php";
       url2 = "http://www.facebook.com";

        curl2 = curl_easy_init();
        if(curl2) {

            string html2 = "";
            struct curl_slist *chunk = NULL;


        curl_easy_setopt(curl2, CURLOPT_URL, url2.c_str());     
        curl_easy_setopt(curl2, CURLOPT_COOKIEFILE, "C:\\teste.txt");
        curl_easy_setopt(curl2, CURLOPT_COOKIEJAR, "C:\\teste.txt");                
        curl_easy_setopt(curl2, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl2, CURLOPT_WRITEDATA, &html2);


        res = curl_easy_perform(curl2);

        std::cout << "RESULT: " << html2;

        }


        _getch();
        return 0;
}

推荐答案

记住要呼叫curl_easy_cleanup(curl2)

这篇关于Cookies不会保存在cURL C ++中,这样的简单干净的代码示例将无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆