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 * Simple HTTP GET that stores the headers in a separate file
30 #include <curl/curl.h>
32 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
34 size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
41 static const char *headerfilename = "head.out";
43 static const char *bodyfilename = "body.out";
46 curl_global_init(CURL_GLOBAL_ALL);
48 /* init the curl session */
49 curl_handle = curl_easy_init();
52 curl_easy_setopt(curl_handle, CURLOPT_URL, "http://example.com");
54 /* no progress meter please */
55 curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
57 /* send all data to this function */
58 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
60 /* open the header file */
61 headerfile = fopen(headerfilename, "wb");
63 curl_easy_cleanup(curl_handle);
67 /* open the body file */
68 bodyfile = fopen(bodyfilename, "wb");
70 curl_easy_cleanup(curl_handle);
75 /* we want the headers be written to this file handle */
76 curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, headerfile);
78 /* we want the body be written to this file handle instead of stdout */
79 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
82 curl_easy_perform(curl_handle);
84 /* close the header file */
87 /* close the body file */
90 /* cleanup curl stuff */
91 curl_easy_cleanup(curl_handle);