local MYHttpRequest = class("MYHttpRequest")
function MYHttpRequest:ctor()
end
HTTP连接使用的是“请求——响应”的方式,不仅在请求时需要先建立连接,而且需要客户端向服务器发出请求后,服务器端才能回复数据
> get是从服务器上获取(查询)数据;而post是向服务器传送(修改)数据。
> get传输数据量较小,2KB;而post传输一般不受限制。
> get安全性非常低,但执行效率比较好;而post安全性相对来说就比较好。
建议:在做数据查询时用get方式,而在做数据添加,修改或删除时用post方式。
--args:
--1、requestType 请求方式 GET or POST
--2、url 请求的地址
--3、params 请求带的参数列表 如 {"userID" = 9527 , "gameID" = "123456"}
--4、sCallback 请求数据成功后回调函数
--5、fCallback 请求数据失败后回调函数
--6、responseType 返回数据类型:
--[[
cc.XMLHTTPREQUEST_RESPONSE_STRING = 0 -- 返回字符串类型
cc.XMLHTTPREQUEST_RESPONSE_ARRAY_BUFFER = 1 -- 返回字节数组类型
cc.XMLHTTPREQUEST_RESPONSE_BLOB = 2 -- 返回二进制大对象类型
cc.XMLHTTPREQUEST_RESPONSE_DOCUMENT = 3 -- 返回文档对象类型
cc.XMLHTTPREQUEST_RESPONSE_JSON = 4 -- 返回JSON数据类型
]]
--7、requestDesc 请求描述
function MYHttpRequest:doRequest(args)
--[[(1)创建HttpRequest请求
(2)设置request请求的url、请求方式GET/POST、发送的数据、请求的响应函数。
(3)发送request请求,HttpClient::getInstance()->send(request); 。
(4)处理请求的响应函数。
(5)释放HTTP连接--]]
dump(args,"MYHttpRequest:doRequest[args]")
--简单校验url
if not args.url then
printInfo("请传入正确的url")
return
end
--请求方式
local requestType = args.requestType or E_REQUEST_TYPE.GET
requestType = string.upper(tostring(requestType))
if requestType ~= E_REQUEST_TYPE.GET and
requestType ~= E_REQUEST_TYPE.POST then
requestType = E_REQUEST_TYPE.GET
end
--返回数据类型
local responseType = args.responseType or cc.XMLHTTPREQUEST_RESPONSE_JSON
if responseType ~= cc.XMLHTTPREQUEST_RESPONSE_STRING and
responseType ~= cc.XMLHTTPREQUEST_RESPONSE_ARRAY_BUFFER and
responseType ~= cc.XMLHTTPREQUEST_RESPONSE_BLOB and
responseType ~= cc.XMLHTTPREQUEST_RESPONSE_DOCUMENT and
responseType ~= cc.XMLHTTPREQUEST_RESPONSE_JSON then
responseType = cc.XMLHTTPREQUEST_RESPONSE_JSON
end
--请求描述
local requestDesc = args.requestDesc
--请求参数
local params = args.params
-- url
local url = args.url
if requestType == E_REQUEST_TYPE.GET then
url = url .. tools.convertGetParams(params)
end
local xhr = cc.XMLHttpRequest:new()-- 创建HTTP请求
xhr.responseType = responseType --返回数据类型
xhr:open(requestType, url) --请求方式
local sCallback = args.sCallback
local fCallback = args.fCallback
local function onResponse()
if requestDesc and removeLoading then
removeLoading()
end
printInfo("xhr.readyState is: %s | xhr.status is: %s",tostring(xhr.readyState),tostring(xhr.status))
if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then
xhr.response = string.gsub(xhr.response, "\\/", "/")
xhr.response = string.gsub(xhr.response, "&", "&")
xhr.response = string.gsub(xhr.response, """, "\"")
local data = xhr.response
printInfo("xhr.response = %s",tostring(xhr.response))
if responseType == cc.XMLHTTPREQUEST_RESPONSE_JSON then
data = json.decode(xhr.response)
-- if data.status ~= G_JSON_DATA_STATUS_OK then
-- createTips(string.format("错误码:%s 错误消息:%s",data.status,tostring(data.msg)))
-- end
elseif responseType == cc.XMLHTTPREQUEST_RESPONSE_STRING then
data = tostring(data)
end
dump(data,string.format("MYHttpRequest:doRequest[%s]",requestDesc or "未定义"))
if sCallback then
sCallback(data)
end
else
if fCallback then
fCallback()
end
end
end
xhr:registerScriptHandler(onResponse)--添加监听,当服务器反馈到客户端时会调用函数onResponse
local sendData = nil
if requestType == E_REQUEST_TYPE.POST then
sendData = tools.convertPostParams(params)
end
xhr:send(sendData) --向服务器发出请求,如果采用异步方式,该方法会立即返回
if requestDesc and createLoading then
createLoading(requestDesc)
end
end
local instance = nil
function MYHttpRequest_getInstance()
if(nil == instance) then
instance = MYHttpRequest:create()
end
return instance
end
function MYHttpRequest_destroyInstance()
instance = nil
end
qs================================
--params 请求参数
url = url .. "?api=".. json.encode(params)
req = network.createHTTPRequest(listener,url,"GET");
local data = request:getResponseString()
if string.find(data,"{") then
data = checktable(json.decode(data))
if not data or (data and not data.svflag) then
print(' http json decode error!!!!!!!')
end
end
=========
https://www.cnblogs.com/ranyonsue/p/5984001.html -- 关于HTTP协议,一篇就够了
cocos Http
最新推荐文章于 2023-11-29 15:46:12 发布