Libcurl library for http Communication-C-based HTTP client

Source: Internet
Author: User

Libcurl library for http Communication-C-based HTTP client

The previous two blogs introduced some of the simplest basic knowledge. Today I will apply it and write an HTTP client.

First, it contains the header file of curl.
Next, two variables are defined for transmission. The first variable is wr_buf, which indicates the buffer zone in which incoming data will be written. Wr_index indicates the index currently written to the buffer.

Let's look at the main function. This function uses easy API to set. All cURL calls are performed by maintaining the handle of a specific request status. This is called a CURL pointer reference. This example also creates a special return code called CURLcode. Before using any libcurl function, you need to call curl_easy_init to obtain the CURL statement handle. Next, pay attention to the number of curl_easy_setopt calls. They configure handles for specific operations. For these calls, you provide handles, commands, and options. First, use CURLOPT_URL in this example to specify the URL to be obtained. Then, it uses CURL_WRITEDATA to provide a context variable (in this example, it is an internal write error variable ). Finally, it uses CURLOPT_WRITEFUNCTION to specify the function to be called when data is available. After the API is started, the API uses the data it reads to call the function multiple times.

To start transmission, call curl_easy_perform. It performs transmission according to the previous configuration. When this function is called, the function will not return until the transfer is completed or an error occurs. The last step of main is to submit the return status, read the submitted page, and use curl_easy_cleanup to clear (after the operation is completed using the handle ).

Now let's look at the write_data function. This function is called when a specific operation receives data. Note: When you read data from a website, the data (write_data) will be written ). A buffer (including available data), number of members, size (total amount of available data in the buffer), and context pointer will be provided to the callback. The first task is to ensure that the buffer space (wr_buf) is sufficient to write data. If not, it sets the context pointer and returns 0, indicating a problem. Otherwise, it copies the data in the cURL buffer to your buffer and adds an index pointing to the next location to be written. In this example, the string is also terminated. You can use printf for it later. Finally, it returns the number of bytes for the libcurl operation. This will tell the libcurl data to be extracted, and it can also discard the data. This is a relatively simple way to read files from the website to the memory.

#include 
  
    #include 
   
     #include "curl.h"#define MAX_BUF      65536 char wr_buf[MAX_BUF + 1];int  wr_index;/** Write data callback function (called within the context of* curl_easy_perform.*/size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp){    int segsize = size * nmemb;    /* Check to see if this data exceeds the size of our buffer. If so,    * set the user-defined context value and return 0 to indicate a    * problem to curl.    */    if (wr_index + segsize > MAX_BUF) {        *(int *)userp = 1;        return 0;    }    /* Copy the data from the curl buffer into our buffer */    memcpy((void *)&wr_buf[wr_index], buffer, (size_t)segsize);    /* Update the write index */    wr_index += segsize;    /* Null terminate the buffer */    wr_buf[wr_index] = 0;    /* Return the number of bytes received, indicating to curl that all is okay */    return segsize;}/** Simple curl application to read the index.html file from a Web site.*/int main(void){    CURL *curl;    CURLcode ret;    int  wr_error;    wr_error = 0;    wr_index = 0;    /* First step, init curl */    curl = curl_easy_init();    if (!curl) {        printf("couldn't init curl ");        return 0;    }    /* Tell curl the URL of the file we're going to retrieve */    curl_easy_setopt(curl, CURLOPT_URL, "www.exampledomain.com");    /* Tell curl that we'll receive data to the function write_data, and    * also provide it with a context pointer for our error return.    */    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&wr_error);    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);    /* Allow curl to perform the action */    ret = curl_easy_perform(curl);    printf("ret = %d (write_error = %d) ", ret, wr_error);    /* Emit the page if curl indicates that no errors occurred */    if (ret == 0) printf("%s ", wr_buf);    curl_easy_cleanup(curl);    return 0;}
   
  

Related Article

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.