Original article, reproduced please indicate the source: http://blog.csdn.net/zhy_cheng/article/details/8664432
You can use cchttprequest to connect to the Internet by using get and post methods. Both get and post can obtain data from the server. The two methods of networking are different. Below is a simple distinction
1. When the GET request is submitted, the data is placed behind the URL, and the Post is placed in the HTTP message body.
2. There is a limit on the size of get data, which is 2 kb and no limit on post theory.
3. Get is less secure than post.
Next let's take a look at how the cocos2d-x transmits data with the server.
First configure the environment
#include "cocos-ext.h"USING_NS_CC_EXT;#pragma comment(lib,"pthreadVCE2.lib");
Right-click the project and choose Properties> C/C ++> General> additional package directory.
Add the following Directory D: \ cocos2d-2.1beta3-x-2.1.0 \ extensions.
Right-click the project and choose Properties> linker> Add dependency.
Add
Libcurl_imp.lib
Libextensions. Lib.
Now, the environment is configured.
First, get requests
Void helloworld: getclicked (ccobject * sender) {// After compiling to the Android platform, add the Internet permission cocos2d: Extension: cchttprequest * request = new cocos2d: extension :: cchttprequest (); // create Object Request-> seturl ("http://www.baidu.com"); // set the request address request-> setrequesttype (cchttprequest: khttpget ); /* request type khttpget, GET request khttppost, POST request khttpunkown Haha */request-> setresponsecallback (this, callfuncnd_selector (helloworld: ongetfinished )); // request-> setrequestdata ("helloworld", strlen ("helloworld ")); // request data request-> settag ("Get Qing Qiu Baidu"); // tag cchttpclient: getinstance ()-> send (request ); // send the request-> release (); // release the memory. The new} is used before}
The post request is as follows:
void HelloWorld::postClicked(CCObject *sender){CCHttpRequest* request = new CCHttpRequest(); request->setUrl("http://www.httpbin.org/post"); request->setRequestType(CCHttpRequest::kHttpPost); request->setResponseCallback(this, callfuncND_selector(HelloWorld::onGetFinished)); // write the post data const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest"; request->setRequestData(postData, strlen(postData)); request->setTag("POST test1"); CCHttpClient::getInstance()->send(request); request->release();}
The ongetfinished function processes the data returned by the server after the request.
Void helloworld: ongetfinished (ccnode * node, void * Data) {cchttpresponse * response = (cchttpresponse *) data; If (! Response) {return;} int S = response-> gethttprequest ()-> getrequesttype (); cclog ("request type % d", S); If (0! = Strlen (response-> gethttprequest ()-> gettag () {cclog ("% s ------> oked", response-> gethttprequest () -> gettag ();} int statuscode = response-> getresponsecode (); cclog ("response code: % d", statuscode); char statusstring [64] = {}; sprintf (statusstring, "HTTP status code: % d, tag = % s", statuscode, response-> gethttprequest ()-> gettag (); cclog (statusstring ); if (! Response-> issucceed () {cclog ("response failed"); cclog ("Error Buffer: % s", response-> geterrorbuffer (); return;} STD :: vector <char> * buffer = response-> getresponsedata (); printf ("HTTP test, dump data:"); For (unsigned int I = 0; I <buffer-> size (); I ++) {cclog ("% C", (* buffer) [I]); // print the data returned from the server here} printf ("\ n ");}
This is the implementation of cchttprequest networking. There is an instance in testcpp, and I will refer to testcpp when implementing it.
Finally, download the code: Click to open the link.