2011-03-10 18:48:02 +08:00
|
|
|
/***************************************************************************
|
2006-11-08 16:49:27 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2001-02-20 21:56:38 +08:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-03-10 18:48:02 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2011-03-10 18:48:02 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2011-03-10 18:48:02 +08:00
|
|
|
***************************************************************************/
|
2016-01-04 22:34:05 +08:00
|
|
|
/* <DESC>
|
|
|
|
* Simple HTTP GET that stores the headers in a separate file
|
|
|
|
* </DESC>
|
|
|
|
*/
|
2000-09-21 06:32:08 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2007-07-17 05:22:12 +08:00
|
|
|
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
|
2000-09-21 06:32:08 +08:00
|
|
|
{
|
2016-02-11 17:21:09 +08:00
|
|
|
size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
|
2000-09-21 06:32:08 +08:00
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2010-12-18 06:34:26 +08:00
|
|
|
int main(void)
|
2000-09-21 06:32:08 +08:00
|
|
|
{
|
|
|
|
CURL *curl_handle;
|
2007-07-13 05:11:10 +08:00
|
|
|
static const char *headerfilename = "head.out";
|
2000-09-21 06:32:08 +08:00
|
|
|
FILE *headerfile;
|
2007-07-13 05:11:10 +08:00
|
|
|
static const char *bodyfilename = "body.out";
|
2000-09-21 06:32:08 +08:00
|
|
|
FILE *bodyfile;
|
|
|
|
|
2003-11-19 16:21:34 +08:00
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
|
2000-09-21 06:32:08 +08:00
|
|
|
/* init the curl session */
|
|
|
|
curl_handle = curl_easy_init();
|
|
|
|
|
|
|
|
/* set URL to get */
|
2018-09-23 17:20:26 +08:00
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_URL, "https://example.com");
|
2000-09-21 06:32:08 +08:00
|
|
|
|
|
|
|
/* no progress meter please */
|
2008-05-23 05:20:07 +08:00
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
|
2000-09-21 06:32:08 +08:00
|
|
|
|
|
|
|
/* send all data to this function */
|
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
|
|
|
|
|
2014-12-31 19:11:34 +08:00
|
|
|
/* open the header file */
|
|
|
|
headerfile = fopen(headerfilename, "wb");
|
|
|
|
if(!headerfile) {
|
2000-09-21 06:32:08 +08:00
|
|
|
curl_easy_cleanup(curl_handle);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-12-31 19:11:34 +08:00
|
|
|
|
|
|
|
/* open the body file */
|
|
|
|
bodyfile = fopen(bodyfilename, "wb");
|
|
|
|
if(!bodyfile) {
|
2000-09-21 06:32:08 +08:00
|
|
|
curl_easy_cleanup(curl_handle);
|
2014-12-31 18:40:28 +08:00
|
|
|
fclose(headerfile);
|
2000-09-21 06:32:08 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-21 04:53:37 +08:00
|
|
|
/* we want the headers be written to this file handle */
|
2014-12-31 19:11:34 +08:00
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, headerfile);
|
2000-09-21 06:32:08 +08:00
|
|
|
|
2013-06-21 04:53:37 +08:00
|
|
|
/* we want the body be written to this file handle instead of stdout */
|
2014-12-31 19:11:34 +08:00
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
|
2002-08-12 19:36:48 +08:00
|
|
|
|
2000-09-21 06:32:08 +08:00
|
|
|
/* get it! */
|
|
|
|
curl_easy_perform(curl_handle);
|
|
|
|
|
|
|
|
/* close the header file */
|
|
|
|
fclose(headerfile);
|
|
|
|
|
2013-06-21 04:53:37 +08:00
|
|
|
/* close the body file */
|
|
|
|
fclose(bodyfile);
|
|
|
|
|
2000-09-21 06:32:08 +08:00
|
|
|
/* cleanup curl stuff */
|
|
|
|
curl_easy_cleanup(curl_handle);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|