1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * This example shows usage of simple cookie interface.
17 #include <curl/curl.h>
20 print_cookies(CURL *curl)
23 struct curl_slist *cookies;
24 struct curl_slist *nc;
27 printf("Cookies, curl knows:\n");
28 res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
29 if (res != CURLE_OK) {
30 fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n", curl_easy_strerror(res));
35 printf("[%d]: %s\n", i, nc->data);
42 curl_slist_free_all(cookies);
51 curl_global_init(CURL_GLOBAL_ALL);
52 curl = curl_easy_init();
56 curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/");
57 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
58 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* just to start the cookie engine */
59 res = curl_easy_perform(curl);
60 if (res != CURLE_OK) {
61 fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
67 printf("Erasing curl's knowledge of cookies!\n");
68 curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
72 printf("-----------------------------------------------\n"
73 "Setting a cookie \"PREF\" via cookie interface:\n");
75 #define snprintf _snprintf
77 /* Netscape format cookie */
78 snprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%lu\t%s\t%s",
79 ".google.com", "TRUE", "/", "FALSE", time(NULL) + 31337, "PREF", "hello google, i like you very much!");
80 res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
81 if (res != CURLE_OK) {
82 fprintf(stderr, "Curl curl_easy_setopt failed: %s\n", curl_easy_strerror(res));
86 /* HTTP-header style cookie */
87 snprintf(nline, sizeof(nline),
88 "Set-Cookie: OLD_PREF=3d141414bf4209321; "
89 "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com");
90 res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
91 if (res != CURLE_OK) {
92 fprintf(stderr, "Curl curl_easy_setopt failed: %s\n", curl_easy_strerror(res));
98 res = curl_easy_perform(curl);
99 if (res != CURLE_OK) {
100 fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
105 fprintf(stderr, "Curl init failed!\n");
109 curl_global_cleanup();