Cocos2d-x中 发起http请求

Cocos2d-x中 发起http请求,第1张

概述bool httpTool::httpRequest(char * tag, char * url, char * requestdata, int requestType) { // 请求示例 //CCHttpRequest* request = new CCHttpRequest(); //request->setUrl(url /*"http://httpbin.org/delete"*/)

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

{

// 请求示例
//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();


//实际代码

if (tag == 0 || tag[0] == '\0' || url == 0 || url[0] = '\0' || requestType < 0 || requestType > (int)network::httpRequest::Type::UNKNowN)

{

return false;

}

//请求的大体思路是

//1.使用network::httpRequest构造要请求的数据

//2.使用network::httpClIEnt使用这个作为发射器把数据发射出去

network::httpRequest * request = new network::httpRequest();

request->setUrl(url); //设置请求地址

request->setRequestType((network::httpRequest::Type)requestType); //设置请求类型 ,有GET,POST,PUT,DELETE,UNKNowN 五钟

if (requestdata != 0 && requestdata[0] != '\0') //如果有请求参数

{

request->setRequestData[requestdata,strlen(requestdata)); //设置请求参数

}

network::cchttpRequestCallback httpRequestCallback = std::bind(&CCSceneEx::onhttpRequestCompleted,this,std::placeholder::-1,std::placeholder::_2);

request->setResponseCallback(httpRequestCallback); //设置回调函数

request->setTag(tag); //设置标签

network::httpClIEnt::getInstance()->send(request); //发送请求

request->release(); //释放请求

}


//httpRequest回调函数

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<char> * 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;

}

}

总结

以上是内存溢出为你收集整理的Cocos2d-x中 发起http请求全部内容,希望文章能够帮你解决Cocos2d-x中 发起http请求所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1036208.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存