sepheaders.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  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.
  13. *
  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.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <curl/curl.h>
  26. static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
  27. {
  28. int written = fwrite(ptr, size, nmemb, (FILE *)stream);
  29. return written;
  30. }
  31. int main(void)
  32. {
  33. CURL *curl_handle;
  34. static const char *headerfilename = "head.out";
  35. FILE *headerfile;
  36. static const char *bodyfilename = "body.out";
  37. FILE *bodyfile;
  38. curl_global_init(CURL_GLOBAL_ALL);
  39. /* init the curl session */
  40. curl_handle = curl_easy_init();
  41. /* set URL to get */
  42. curl_easy_setopt(curl_handle, CURLOPT_URL, "http://example.com");
  43. /* no progress meter please */
  44. curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
  45. /* send all data to this function */
  46. curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
  47. /* open the files */
  48. headerfile = fopen(headerfilename,"wb");
  49. if (headerfile == NULL) {
  50. curl_easy_cleanup(curl_handle);
  51. return -1;
  52. }
  53. bodyfile = fopen(bodyfilename,"wb");
  54. if (bodyfile == NULL) {
  55. curl_easy_cleanup(curl_handle);
  56. return -1;
  57. }
  58. /* we want the headers be written to this file handle */
  59. curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, headerfile);
  60. /* we want the body be written to this file handle instead of stdout */
  61. curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
  62. /* get it! */
  63. curl_easy_perform(curl_handle);
  64. /* close the header file */
  65. fclose(headerfile);
  66. /* close the body file */
  67. fclose(bodyfile);
  68. /* cleanup curl stuff */
  69. curl_easy_cleanup(curl_handle);
  70. return 0;
  71. }