Http post easy client

Source: Internet
Author: User
Tags sendmsg

C API provides two APIs in the libcurl function. The easy interface is a simple synchronous API (meaning that when you use a request to call libcurl, it will be able to meet your request until it is completed or an error occurs ). Multiple Interfaces further control the libcurl. Your application can perform multiple synchronous transmission operations and control the time and place when libcurl moves data.

This example uses the easy interface. This API can also control the data movement process (using callback), but it is very simple to use, as shown in its name. Listing 3 provides an http c language example.


Listing 3. c HTTP client using libcurl easy interface

 #include <stdio.h>  #include <string.h>  #include <curl/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/n");     return 0;   }   /* Tell curl the URL of the file we're going to retrieve */   curl_easy_setopt( curl, CURLOPT_URL, "[url]www.exampledomain.com"[/url] );   /* 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)/n", ret, wr_error );   /* Emit the page if curl indicates that no errors occurred */   if ( ret == 0 ) printf( "%s/n", wr_buf );   curl_easy_cleanup( curl );   return 0;  } 


The top is requiredincludeFile, including the curl root file. Next, I defined two variables for transmission. The first variable iswr_bufIndicates the buffer in which incoming data is written.wr_indexIndicates the index currently written to the buffer.

GomainFunction, which uses easy API to set. All curl calls pass {
Sendmsg ('PW _ Ajax. php', 'Action = relatetag & tagname = maintain', this. ID)
} "> Maintain the handle of a specific request status. This is calledCURLPointer Reference. This example also creates a special return code calledCURLcode. Before using any libcurl function, you must callcurl_easy_initObtainCURLHandle. Next, note thatcurl_easy_setoptNumber of calls. They configure handles for specific operations. For these calls, you provide handles, commands, and options. First, this example usesCURLOPT_URLSpecify the URL to be obtained. Then, it usesCURL_WRITEDATAProvides a context variable (in this example, it is an internal write error variable ). Finally, it usesCURLOPT_WRITEFUNCTIONSpecifies 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, callcurl_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.mainThe last step is to submit the return status, read the submission page, and usecurl_easy_cleanupClear (after the operation is completed using the handle ).

Now lookwrite_dataFunction. This function is called when a specific operation receives data. Note that when you read data from a website (write_data). 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 (wr_buf{
Sendmsg ('PW _ Ajax. php', 'Action = relatetag & tagname = space', this. ID)
} "> Enough space 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 and can be used later.printf. 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.


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.