Use the simple interface pattern to design an HTTP-access application that obtains HTML files over the network.
#include <stdio.h> #include <stdlib.h> #include <curl/curl.h> Contains Libcurl library file *FP; Defines a file identifier size_t write_data (void *ptr, size_t size, size_t nmemb, Voi D *stream) {//define callback function to write HTML file to local int written = Fwrite (ptr, size, NMEMB, (FILE *) FP); return written;} int main (int argc, char *argv[]) {//Run parameter first is URL, second is local file path CURL *curl; Defines the Curl identifier pointer curl_global_init (Curl_global_all); Initialize Libcurl curl = Curl_easy_init (); Create a Curl identifier curl_easy_setopt (curl, Curlopt_url, argv[1]); Set the first run parameter to the URL to be accessed if (fp = fopen (Argv[2], "w") = = NULL) {//To create the file as a path with the second parameter set puts ("Enter the correct form to save File name "); Curl_easy_cleanup (curl); Clear Curl Identifier return 1 on error; } curl_easy_setopt (Curl, curlopt_writefunction, write_data); Set the callback function name Curl_easy_perform (curl); Start data transfer, execute callback Curl_easy_cleanup (curl) after end; Clears the curl identifier return 0;}
Using Libcurl to implement simple HTTP access