1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * Import and export cookies with COOKIELIST.
33 #include <curl/curl.h>
36 print_cookies(CURL *curl)
39 struct curl_slist *cookies;
40 struct curl_slist *nc;
43 printf("Cookies, curl knows:\n");
44 res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
46 fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n",
47 curl_easy_strerror(res));
52 printf("[%d]: %s\n", i, nc->data);
59 curl_slist_free_all(cookies);
68 curl_global_init(CURL_GLOBAL_ALL);
69 curl = curl_easy_init();
73 curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/");
74 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
75 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* start cookie engine */
76 res = curl_easy_perform(curl);
78 fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
84 printf("Erasing curl's knowledge of cookies!\n");
85 curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
89 printf("-----------------------------------------------\n"
90 "Setting a cookie \"PREF\" via cookie interface:\n");
92 #define snprintf _snprintf
94 /* Netscape format cookie */
95 snprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%lu\t%s\t%s",
96 ".google.com", "TRUE", "/", "FALSE",
97 (unsigned long)time(NULL) + 31337UL,
98 "PREF", "hello google, i like you very much!");
99 res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
100 if(res != CURLE_OK) {
101 fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
102 curl_easy_strerror(res));
106 /* HTTP-header style cookie. If you use the Set-Cookie format and don't
107 specify a domain then the cookie is sent for any domain and will not be
108 modified, likely not what you intended. Starting in 7.43.0 any-domain
109 cookies will not be exported either. For more information refer to the
110 CURLOPT_COOKIELIST documentation.
112 snprintf(nline, sizeof(nline),
113 "Set-Cookie: OLD_PREF=3d141414bf4209321; "
114 "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com");
115 res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
116 if(res != CURLE_OK) {
117 fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
118 curl_easy_strerror(res));
124 res = curl_easy_perform(curl);
125 if(res != CURLE_OK) {
126 fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
130 curl_easy_cleanup(curl);
133 fprintf(stderr, "Curl init failed!\n");
137 curl_global_cleanup();