First, how to use
//Send InterfacevoidCmdhelper::p ostrequest (Const Char* Cmdtag,Const Char* URL,Const Char*postdata, size_t postdatalengt) {Cchttprequest* Request =Newcchttprequest (); Request-Settag (Cmdtag); Request-seturl (URL); Request->setresponsecallback ( This, Httpresponse_selector (CMDHELPER::ONREQUESTRSP)); //Set Request Type:get/post/put/deleteRequest->Setrequesttype (cchttprequest::khttppost); //Set HeaderSTD::VECTOR<STD::string>headers; Headers.push_back ("Content-type:application/json; Charset=utf-8"); Request-setheaders (headers); //set body data, POST onlyRequest->Setrequestdata (PostData, postdatalengt); //Set Time OutCchttpclient::getinstance ()Settimeoutforconnect (Timeout_connect); Cchttpclient::getinstance ()-Settimeoutforread (Timeout_read); Cchttpclient::getinstance ()-Send (Request); //don ' t forget to release it, pair to newRequest->release ();}#defineCmd_download "Cmd_download"#defineUrl_download "http://192.168.2.122:9001/star/downsolution "std::stringdata ="";//when sending a POST request, call the following interface topostrequest (Cmd_download, Url_download, Data.c_str (), data.length ());//receiving callbacksvoidCMDHELPER::ONREQUESTRSP (cchttpclient* client, cchttpresponse*response) { if(!response) {Cclog ("ERROR:%s---> NULL response", __function__); return; } std::stringTag = Response->gethttprequest ()Gettag (); intStatusCode = response->Getresponsecode (); Cclog ("%s---> Response code:%d, tag =%s", __function__, StatusCode, Tag.c_str ()); STD::stringDatabody; if(!response->issucceed ()) {Cclog ("Response failed"); Cclog ("Error Buffer:%s", response->Geterrorbuffer ()); //This->setlastnetworkerror (Response->geterrorbuffer ()); } Else { //Dump Datastd::vector<Char> *buffer = response->Getresponsedata (); Databody.assign (Buffer->begin (), buffer->end ()); //Cclog ("%s", Databody.c_str ()); } //Dispatch if(Cmd_download = =tag) { This-ondownloadtasksolutionrsp (Databody.c_str (), databody.size ()); } Else{Cclog ("%s---> Error branch, cmd tag =%d", __function__, Tag.c_str ()); }}
Second, the pits encountered
1. Note the encoded format of the transmission
As in the example above, we used the UTF-8 encoding, but when the HTTP transmission of binary data, the server received a large number of data, so either the transmission of the encoding format, or the local to transfer the binary data transcoding (such as our use of base64).
Using HTTP in Cocos2d-x