http://www.jb51.cc/article/p-bxigsxci-no.html
CCHttpRequest的用法很��
1、Get方法,�置URL,��事件:
[html] view plaincopyprint?
--[[
�置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()
--[[
�置ulr:"http://blog.csdn.net/longolder"
��:self:onResponseGet(event)
方法:GET
--]]
local url = "http://blog.csdn.net/longolder"
local request = network.createHTTPRequest(function(event)
self:onResponseGet(event)
end,"GET")
request:setTimeout(30)
request:start()
默�超����10s,���用setTimeout�置30s超�
--�用addPOSTValue向Form中添加�值�例如:key:value
forkey,valueinpairs(dataInfo)do
request:addPOSTValue(key,value)
end
-- �用addPOSTValue向Form中添加�值�例如:key:value
for key,value in pairs(dataInfo) do
request:addPOSTValue(key,value)
end
dataInfo是一�保存要�送��的table集合
下面是回�方法:
functionHttpRequestTest:onResponseGet(event)
localrequest=event.request
ifevent.name~="completed"then--��completed表示正常�束此事件
print("request:getErrorCode(),request:getErrorMessage()",request:getErrorCode(),request:getErrorMessage())
return
end
localcode=request:getResponseStatusCode()
ifcode~=200then--成功
print("code",code)
localstrResponse=string.trim(request:getResponseString())
print(strResponse)
function HttpRequestTest:onResponseGet(event) local request = event.request if event.name ~= "completed" then -- ��completed表示正常�束此事件 print("request:getErrorCode(),request:getErrorMessage() ",request:getErrorMessage()) return end local code = request:getResponseStatusCode() if code ~= 200 then -- 成功 print("code ",code) return end local strResponse = string.trim(request:getResponseString()) print(strResponse) end
常用的���有以下��值:
200 - 服�器成功返回��
404 - �求的��不存在
503 - 服�器��不可用