Http网络交互
Cocos2dx 为我们封装了在cocos2dx中http的网络框架,其文件在cocos2dx引擎包的/Users/Himi/Documents/cocos2d-2.1rc0-x-2.1.2-hotfix/extensions/network 文件下的 HttpClient、HttpRequest 、HttpResponse如下图:
至于如何使用,这里不再详细叙述,大家可以看引擎提供的例子中自己阅读学习(TestCpp项目中)。
-- Get url
CCHttpRequest:open("www.baidu.com", kHttpGet):sendWithHandler(
function(res, hnd)
print(res:getResponseData(), res:getResponseCode())
end)
-- Post url
CCHttpRequest:open("www.baidu.com", kHttpPost, "query=param1&other=params"):sendWithHandler(
function(res, hnd)
print(res:getResponseData(), res:getResponseCode())
end)
- CCHttpRequest:open(agr0,agr1)函数:
函数第一个参数:获取数据的url地址
函数第二个参数: http交互方式 GET or POST
- sendWithHandler函数:发送请求,监听返回信息。
- 在function(res, hnd)函数:
res:getResponseData() --获取http响应原始数据
res:getResponseCode() --获取http响应错误代码
我自己用post去访问百度,返回的信息,如图
呵呵,返回一个html信息。这个不用理了,重要是大家能过这种方式访问对应服务器,做c/s交互吧。。。。
如有错误之处,希望大家多多纠正!
转载请注明出处:http://blog.csdn.net/rexuefengye/article/details/16357655