Cocos2d-x curl simple demo

Source: Internet
Author: User


I have reproduced two articles on curl in detail. To be honest, I am not very familiar with it. The following is an example of the engine.

(1) For the curl class file location, you can see the curl-related files in libs/cocos2dx/platform/third_party/IOS/curl.

(2) Example of self-contained engine: curltest

The Code related to curl is as follows:

void CurlTest::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent){    CURL *curl;    CURLcode res;    char buffer[10];    curl = curl_easy_init();    if (curl)    {        curl_easy_setopt(curl, CURLOPT_URL, "www.google.com");        res = curl_easy_perform(curl);        /* always cleanup */        curl_easy_cleanup(curl);        if (res == 0)        {            m_pLabel->setString("0 response");        }        else        {            sprintf(buffer,"code: %i",res);            m_pLabel->setString(buffer);        }            }    else     {        m_pLabel->setString("no curl");    } }

(3) The following is a similar code:

Note: curl_global_init (curl_global_default)
-- Global initialization function, called only once in the program. Parameters include:

#define CURL_GLOBAL_SSL (1<<0)#define CURL_GLOBAL_WIN32 (1<<1)#define CURL_GLOBAL_ALL (CURL_GLOBAL_SSL|CURL_GLOBAL_WIN32)#define CURL_GLOBAL_NOTHING 0#define CURL_GLOBAL_DEFAULT CURL_GLOBAL_ALL


// Define the Data Writing callback function. However, in the callback function, only the obtained data is output. size_t write_data (void * buffer, size_t size, size_t nmemb, void * stream) {printf ("% s ", (char *) buffer); Return size * nmemb; // the actual number of bytes returned must be returned here} // modify the function processing after the touch screen ends. Void helloworld: cctouchesended (ccset * ptouches, ccevent * pevent) {// first, you must create a culr pointer to use curl, which is the global handle of curl. Curl * curl; // the return value of an API in the curl library, which is used to obtain the API call result. Curlcode res; // Step 1: Initialize curl and obtain the curl pointer after successful initialization. Curl = curl_easy_init (); If (curl) {// step 2, set the action we use this curl pointer to complete. Parameter 1 is the curl pointer, and parameter 2 is the corresponding action type enumeration. The enumerated value is curl. h, for example, curlopt_url in this example, is defined as cinit (URL, objectpoint, 2), that is, the HTTP service connected to a website. Parameter 3 is the data parameter corresponding to the action. Here is the URL address of the website. Curl_easy_setopt (curl, curlopt_url, "http://www.baidu.com"); // curl_easy_setopt // sets the callback function for writing data. Res = curl_easy_setopt (curl, curlopt_writefunction, write_data); If (res! = Curle_ OK) {printf ("curl_easy_setopt not return curle_ OK \ n");} else {printf ("curl_easy_setopt exec success \ n");} // step 3, execute the action. Put the returned results in res. Res = curl_easy_perform (curl); If (res! = Curle_ OK) {printf ("curl_easy_perform not return curle_ OK \ n");} else {printf ("curl_easy_perform exec success \ n ");} /* always cleanup * // The last step is to clear the curl pointer and end the use of the curl library. Curl_easy_cleanup (curl );}}

Explain this code: start the program, click the screen, and trigger the touchended function. In this function, you can connect to www.baidu.com and output the obtained data in the callback method, save the data as an HTML file. Double-click the file on the Baidu homepage.

Game operation terminal output:

curl_easy_setopt exec success

<! Doctype HTML> <! -- Status OK --> <HTML> 

curl_easy_perform exec success

Conclusion: To tell the truth, the corresponding curl does not understand it. It's just getting started and I'm a bit impressed. I'll try it later to get a better understanding!

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.