cookie_interface.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * This example shows usage of simple cookie interface.
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <errno.h>
  14. #include <time.h>
  15. #include <curl/curl.h>
  16. static void
  17. print_cookies(CURL *curl)
  18. {
  19. CURLcode res;
  20. struct curl_slist *cookies;
  21. struct curl_slist *nc;
  22. int i;
  23. printf("Cookies, curl knows:\n");
  24. res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
  25. if (res != CURLE_OK) {
  26. fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n", curl_easy_strerror(res));
  27. exit(1);
  28. }
  29. nc = cookies, i = 1;
  30. while (nc) {
  31. printf("[%d]: %s\n", i, nc->data);
  32. nc = nc->next;
  33. i++;
  34. }
  35. if (i == 1) {
  36. printf("(none)\n");
  37. }
  38. curl_slist_free_all(cookies);
  39. }
  40. int
  41. main(void)
  42. {
  43. CURL *curl;
  44. CURLcode res;
  45. curl_global_init(CURL_GLOBAL_ALL);
  46. curl = curl_easy_init();
  47. if (curl) {
  48. char nline[256];
  49. curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com/"); /* google.com sets "PREF" cookie */
  50. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  51. curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* just to start the cookie engine */
  52. res = curl_easy_perform(curl);
  53. if (res != CURLE_OK) {
  54. fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
  55. return 1;
  56. }
  57. print_cookies(curl);
  58. printf("Erasing curl's knowledge of cookies!\n");
  59. curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
  60. print_cookies(curl);
  61. printf("-----------------------------------------------\n"
  62. "Setting a cookie \"PREF\" via cookie interface:\n");
  63. #ifdef WIN32
  64. #define snprintf _snprintf
  65. #endif
  66. /* Netscape format cookie */
  67. snprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%lu\t%s\t%s",
  68. ".google.com", "TRUE", "/", "FALSE", time(NULL) + 31337, "PREF", "hello google, i like you very much!");
  69. res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
  70. if (res != CURLE_OK) {
  71. fprintf(stderr, "Curl curl_easy_setopt failed: %s\n", curl_easy_strerror(res));
  72. return 1;
  73. }
  74. /* HTTP-header style cookie */
  75. snprintf(nline, sizeof(nline),
  76. "Set-Cookie: OLD_PREF=3d141414bf4209321; "
  77. "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com");
  78. res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
  79. if (res != CURLE_OK) {
  80. fprintf(stderr, "Curl curl_easy_setopt failed: %s\n", curl_easy_strerror(res));
  81. return 1;
  82. }
  83. print_cookies(curl);
  84. res = curl_easy_perform(curl);
  85. if (res != CURLE_OK) {
  86. fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
  87. return 1;
  88. }
  89. }
  90. else {
  91. fprintf(stderr, "Curl init failed!\n");
  92. return 1;
  93. }
  94. curl_global_cleanup();
  95. return 0;
  96. }