Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030 #include <errno.h>
00031 #include <time.h>
00032
00033 #include <curl/curl.h>
00034
00035 static void
00036 print_cookies(CURL *curl)
00037 {
00038 CURLcode res;
00039 struct curl_slist *cookies;
00040 struct curl_slist *nc;
00041 int i;
00042
00043 printf("Cookies, curl knows:\n");
00044 res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
00045 if(res != CURLE_OK) {
00046 fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n",
00047 curl_easy_strerror(res));
00048 exit(1);
00049 }
00050 nc = cookies, i = 1;
00051 while(nc) {
00052 printf("[%d]: %s\n", i, nc->data);
00053 nc = nc->next;
00054 i++;
00055 }
00056 if(i == 1) {
00057 printf("(none)\n");
00058 }
00059 curl_slist_free_all(cookies);
00060 }
00061
00062 int
00063 main(void)
00064 {
00065 CURL *curl;
00066 CURLcode res;
00067
00068 curl_global_init(CURL_GLOBAL_ALL);
00069 curl = curl_easy_init();
00070 if(curl) {
00071 char nline[256];
00072
00073 curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/");
00074 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
00075 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
00076 res = curl_easy_perform(curl);
00077 if(res != CURLE_OK) {
00078 fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
00079 return 1;
00080 }
00081
00082 print_cookies(curl);
00083
00084 printf("Erasing curl's knowledge of cookies!\n");
00085 curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
00086
00087 print_cookies(curl);
00088
00089 printf("-----------------------------------------------\n"
00090 "Setting a cookie \"PREF\" via cookie interface:\n");
00091 #ifdef WIN32
00092 #define snprintf _snprintf
00093 #endif
00094
00095 snprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%lu\t%s\t%s",
00096 ".google.com", "TRUE", "/", "FALSE",
00097 (unsigned long)time(NULL) + 31337UL,
00098 "PREF", "hello google, i like you very much!");
00099 res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
00100 if(res != CURLE_OK) {
00101 fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
00102 curl_easy_strerror(res));
00103 return 1;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112 snprintf(nline, sizeof(nline),
00113 "Set-Cookie: OLD_PREF=3d141414bf4209321; "
00114 "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com");
00115 res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
00116 if(res != CURLE_OK) {
00117 fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
00118 curl_easy_strerror(res));
00119 return 1;
00120 }
00121
00122 print_cookies(curl);
00123
00124 res = curl_easy_perform(curl);
00125 if(res != CURLE_OK) {
00126 fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
00127 return 1;
00128 }
00129
00130 curl_easy_cleanup(curl);
00131 }
00132 else {
00133 fprintf(stderr, "Curl init failed!\n");
00134 return 1;
00135 }
00136
00137 curl_global_cleanup();
00138 return 0;
00139 }