Initiate http request in Cocos2d-x

Source: Internet
Author: User

Initiate http request in Cocos2d-x

Bool httpTool: httpRequest (char * tag, char * url, char * requestdata, int requestType)

{

// Request example
// CCHttpRequest * request = new CCHttpRequest ();
// Request-> setUrl (url/* "http://httpbin.org/delete "*/);
// Request-> setRequestType (CCHttpRequest: HttpRequestType) requestType/* CCHttpRequest: kHttpDelete */);
// Request-> setResponseCallback (this, httpresponse_selector (CCSceneEx: onHttpRequestCompleted ));
// Request-> setTag (tag/* "DELETE test2 "*/);
// CCHttpClient: getInstance ()-> send (request );
// Request-> release ();


// Actual code

If (tag = 0 | tag [0] = '\ 0' | url = 0 | url [0] =' \ 0' | requestType <0 | requestType> (int) network: HttpRequest: Type: UNKNOWN)

{

Return false;

}

// The general request logic is

// 1. Use network: HttpRequest to construct the data to be requested

// 2. Use network: HttpClient to use this as the transmitter to transmit data

Network: HttpRequest * request = new network: HttpRequest ();

Request-> setUrl (url); // you can specify the request address.

Request-> setRequestType (network: HttpRequest: Type) requestType); // set the request Type, which can be GET, POST, PUT, DELETE, and UNKNOWN for five minutes

If (requestdata! = 0 & requestdata [0]! = '\ 0') // if there is a request parameter

{

Request-> setRequestData [requestdata, strlen (requestdata); // you can specify the request parameters.

}

Network: ccHttpRequestCallback httpRequestCallback = std: bind (& CCSceneEx: onHttpRequestCompleted, this, std: placeholder:-1, std: placeholder: _ 2 );

Request-> setResponseCallback (httpRequestCallback); // you can specify the callback function.

Request-> setTag (tag); // you can specify a tag.

Network: HttpClient: getInstance ()-> send (request); // send a request

Request-> release (); // release the request

}


// HttpRequest callback function

Void httpTool: onHttpRequestCompleted (network: HttpClient * sender, network: HttpResponse * response)

{

If (! Response)

{

Return;

}

Const char * pTag = response-> getHttpRequest ()-> getTag ();

If (0! = Strlen (pTag ))

{

}

Int ret = 0;

Int statusCode = response-> getResponseCode ();

Char * buffer = 0;

Int bufferSize = 0;

Char * errorInfo = "";

If (response-> isSucceed ())

{

Ret = 1;

Std: vector * Vbuffer = response-> getResponseData ();

BufferSize = vbuffer-> size ();

Buffer = new char [bufferSize + 1];

For (int I = 0; I <bufferSize; ++ I)

{

Buffer [I] = (* vbuffer) [I];

}

Buffer [bufferSize] = '\ 0 ';

}

Else

{

Ret = 0;

ErrorInfo = (char *) response-> getErrorBuffer ();

}

CLuaParam luaParams [6];

LuaParams [0] = ret;

LuaParams [1] = statusCode;

LuaParams [2] = pTag;

LuaParams [3] = bufer;

LuaParams [4] = bufferSize;

LuaParams [5] = errorInfo;


LuaCppData: sharedLuaCppData ()-> RunLuaFunction ("NetUpdate_OnHttpRequestCompleted", luaParams, 6 );

If (buffer! = 0)

{

Delete [] buffer;

}

}

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.