1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, 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 http://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 ***************************************************************************/
22 /* This example shows usage of simple cookie interface. */
30 #include <curl/curl.h>
33 print_cookies(CURL *curl)
36 struct curl_slist *cookies;
37 struct curl_slist *nc;
40 printf("Cookies, curl knows:\n");
41 res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
42 if (res != CURLE_OK) {
43 fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n", curl_easy_strerror(res));
48 printf("[%d]: %s\n", i, nc->data);
55 curl_slist_free_all(cookies);
64 curl_global_init(CURL_GLOBAL_ALL);
65 curl = curl_easy_init();
69 curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/");
70 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
71 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* just to start the cookie engine */
72 res = curl_easy_perform(curl);
73 if (res != CURLE_OK) {
74 fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
80 printf("Erasing curl's knowledge of cookies!\n");
81 curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
85 printf("-----------------------------------------------\n"
86 "Setting a cookie \"PREF\" via cookie interface:\n");
88 #define snprintf _snprintf
90 /* Netscape format cookie */
91 snprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%lu\t%s\t%s",
92 ".google.com", "TRUE", "/", "FALSE", (unsigned long)time(NULL) + 31337UL, "PREF", "hello google, i like you very much!");
93 res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
94 if (res != CURLE_OK) {
95 fprintf(stderr, "Curl curl_easy_setopt failed: %s\n", curl_easy_strerror(res));
99 /* HTTP-header style cookie */
100 snprintf(nline, sizeof(nline),
101 "Set-Cookie: OLD_PREF=3d141414bf4209321; "
102 "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com");
103 res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
104 if (res != CURLE_OK) {
105 fprintf(stderr, "Curl curl_easy_setopt failed: %s\n", curl_easy_strerror(res));
111 res = curl_easy_perform(curl);
112 if (res != CURLE_OK) {
113 fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
118 fprintf(stderr, "Curl init failed!\n");
122 curl_global_cleanup();