加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Quick-Cocos2d-x CCHttpRequest详细说明及用法

发布时间:2020-12-14 20:08:38 所属栏目:百科 来源:网络整理
导读:CCHttpRequest的用法很简单。 1、Get方法,设置URL,响应事件 : 1 2 3 4 5 6 7 8 9 10 11 --[[ 设置ulr: "http://blog.csdn.net/longolder" 监听:self:onResponseGet(event) 方法:GET --]] localurl= "http://blog.csdn.net/longolder" localrequest=net

CCHttpRequest的用法很简单。


1、Get方法,设置URL,响应事件

1
2
3
4
5
6
7
8
9
10
11
--[[
设置ulr: "http://blog.csdn.net/longolder"
监听:self:onResponseGet(event)
方法:GET
--]]
localurl= "http://blog.csdn.net/longolder"
localrequest=network.createHTTPRequest(function(event)
self:onResponseGet(event)
end,url, "GET" )
request:setTimeout(30)
request:start()


默认超时时间为10s,这里调用setTimeout设置30s超时。


2、POST方法,与GET方法相似,只不过要另外添加数据

1
2
3
4
--调用addPOSTValue向Form中添加键值对例如:key:value
for key,valueinpairs(dataInfo) do
request:addPOSTValue(key,value)
end

dataInfo是一个保存要发送数据的table集合。


下面是回调方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
functionHttpRequestTest:onResponseGet(event)
localrequest=event.request
if event.name~= "completed" then--當為completed表示正常結束此事件
print( "request:getErrorCode(),request:getErrorMessage()" ,request:getErrorCode(),request:getErrorMessage())
return
end
localcode=request:getResponseStatusCode()
if code~=200then--成功
print( "code" ,code)
return
end
localstrResponse=string.trim(request:getResponseString())
print(strResponse)
end


常用的状态代码有以下几个值:

200 - 服务器成功返回网页;

404 - 请求的网页不存在;

503 - 服务器暂时不可用;


来源网址:http://www.52php.cn/article/p-bxigsxci-no.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读