Using Libcurl to implement simple HTTP access

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.