In-depth analysis of CurlTest in Cocos2d-x

Source: Internet
Author: User

An in-depth analysis of CurlTest in the Cocos2d-x and the Cocos2d-x version used in this chapter is: Cocos2d-html5-v2.1.1 http://cn.cocos2d-x.org/download Curl definition: curl is a command line tool fortransferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, http post, HTTPPUT, FTP uploading, HTTP form based upload, proxies, cookies, user + passwordauthentication (Basic, Digest, NTLM, Negotiate, kerberos ...), File transferresume, proxy tunneling and a busload of other useful tricks. Hello everyone, let's introduce such a thing today. It is called Curl and is an API that can access various network protocol services. The official website: http://curl.haxx.se/ Although I met it for the first time, I was reminded in bold on the official website: Possibly more than 500 million users already, I'm sure you canuse it as well! The Cocos2d-x adds curl as a third-party library to the curl directory of cocos2dx \ platform \ third_party \ win32. There is a demo CurlTest in TestCpp. It simply uses Curl to access the HTTP server. Let's take a look and open the CurlTest directory under the TestCpp project. There are two files: CurlTest. h/cpp. Let's take a look at CurlTest. h: [cpp] // The layer Used for demonstration. Class CurlTest: public CCLayer {public: // construct CurlTest (); // destructor ~ CurlTest (); // call virtual void ccTouchesEnded (cocos2d: CCSet * pTouches, cocos2d: CCEvent * pEvent) when the touch screen ends; private: // text label cocos2d :: CCLabelTTF * m_pLabel;}; // class CurlTestScene: public TestScene {public: // The function called when the current scenario is run. Virtual void runThisTest () ;}; the corresponding CPP: [cpp] // Add the corresponding header file. # Include "CurlTest. h" # include "stdio. h" # include "stdlib. h" // the header file of the curl library is added here. # Include "curl/curl. h" // constructor. CurlTest: CurlTest () {// obtain the screen size. CCSize s = CCDirector: shareddire()-> getWinSize (); // create a text tag. CCLabelTTF * label = CCLabelTTF: create ("Curl Test", "Arial", 28); // Add the label to the current layer. AddChild (label, 0); // sets the position label-> setPosition (ccp (s. width/2, s. height-50); // enable the touch screen setTouchEnabled (true); // create a text label for displaying the title. M_pLabel = CCLabelTTF: create ("Touch the screen to connect", "Arial", 22); // set the label to center. M_pLabel-> setPosition (ccp (s. width/2, s. height/2); // Add the tag to the current layer. AddChild (m_pLabel, 0); // occupies it and adds one to its reference counter. M_pLabel-> retain ();} // The function called when the touch screen ends. Void CurlTest: ccTouchesEnded (CCSet * pTouches, CCEvent * pEvent) {// here are all statements about CURL. // 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; // character array, used to store return values. Char buffer [10]; // 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, "www.baidu.com"); // Step 3: Execute the action set above. Put the returned results in res. Res = curl_easy_perform (curl); // In the last step, clear the CURL pointer and end the use of the CURL library. Curl_easy_cleanup (curl); // if the action is processed successfully, the connection is successful, but no data is received. If (res = 0) {m_pLabel-> setString ("0 response");} else {// if the action fails to be processed, print the error code. Sprintf (buffer, "code: % I", res); m_pLabel-> setString (buffer) ;}} else {// If initialization fails, no CURL m_pLabel-> setString ("no curl") ;}/// destructor is displayed. CurlTest ::~ CurlTest () {// subtract one from the title text tag counter. It is no longer used for normal release. M_pLabel-> release ();} // run the function called in the current demo scenario. Void CurlTestScene: runThisTest () {// create a layer for demonstration. CCLayer * pLayer = new CurlTest (); addChild (pLayer); // run the current scenario. CCDirector: sharedDirector ()-> replaceScene (this); pLayer-> release ();} to better display the action, I open HTTP Analyzer to capture messages. When I click on the screen, HTTP Analyzer will capture the corresponding HTTP access records. As you can see, this program sends a get message to www.baidu.com. This is just a simple demonstration of the connection process, and it is very good. Now let's have some fun. For example, how can I download the obtained webpage? CURL provides the corresponding action types to obtain data and write data to a file. First, we need to create a file pointer, and then use the CURL action setting function to specify the file pointer used to write the file, then we create a Data Writing Function and set it to the callback function for CURL Data Writing. [Cpp] // defines the callback function for writing data. Size_t write_callback (void * ptr, size_t size, size_t nmemb, FILE * stream) {size_t written = fwrite (ptr, size, nmemb, (FILE *) stream); return written ;} // modify the function processing after the touch screen ends. Void CurlTest: ccTouchesEnded (CCSet * pTouches, CCEvent * pEvent) {CURL * curl; CURLcode res; char buffer [10]; curl = curl_easy_init (); if (curl) {// create the written file. FILE * outfile; outfile = fopen ("C: \ baidu.html", "wb"); curl_easy_setopt (curl, CURLOPT_URL, "www.baidu.com"); if (outfile) {// specify the written file pointer. Curl_easy_setopt (curl, CURLOPT_FILE, outfile);} // sets the callback function for writing data. Curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_callback); res = curl_easy_perform (curl); curl_easy_cleanup (curl); // close the file fclose (outfile); if (res = 0) {m_pLabel-> setString ("0 response") ;}else {sprintf (buffer, "code: % I", res); m_pLabel-> setString (buffer );}} after else {m_pLabel-> setString ("no curl") ;}} runs, we click the screen and we can find that a baidu.html is generated under the C drive. After clicking it, it will look like the following:

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.