如何通过Lua语言请求接口拿到数据
文章目录
-
- 概要
- http客户端
- 通过请求下载数据
概要
当某个需求是需要在模块内请求接口拿到数据,需要使用http客户端调用接口
http客户端
LuaSOC请求接口官方文档
调用:http.request(method,url,headers,body,opts,ca_file,client_ca, client_key, client_password)
参数:
传入值类型 | 解释 |
---|---|
string | 请求方法, 支持 GET/POST 等合法的HTTP方法 |
string | url地址, 支持 http和https, 支持域名, 支持自定义端口 |
tabal | 请求头 可选 例如 {[“Content-Type”] = “application/x-www-form-urlencoded”} |
string/zbuff | body 可选 |
table | 额外配置 可选 包含 timeout:超时时间单位ms 可选,默认10分钟,写0即永久等待 dst:下载路径,可选 adapter:选择使用网卡,可选 debug:是否打开debug信息,可选,ipv6:是否为ipv6 默认不是,可选 callback:下载回调函数,参数 content_len:总长度 body_len:以下载长度 userdata 用户传参,可选 userdata:回调自定义传参 |
string | 服务器ca证书数据, 可选, 一般不需要 |
string | 客户端ca证书数据, 可选, 一般不需要, 双向https认证才需要 |
string | 客户端私钥加密数据, 可选, 一般不需要, 双向https认证才需要 |
string | 客户端私钥口令数据, 可选, 一般不需要, 双向https认证才需要 |
返回值:
返回值类型 | 解释 |
---|---|
int | code , 服务器反馈的值>=100, 最常见的是200.如果是底层错误,例如连接失败, 返回值小于0 |
tabal | headers 当code>100时, 代表服务器返回的头部数据 |
string/int | body 服务器响应的内容字符串,如果是下载模式, 则返回文件大小 |
headers请求头有就写没有就空的,body参数也是的
GET请求
http.request("GET","http://www.baidu.com",{},"",opts)
POST请求
http.request("POST","http://www.baidu.com",{},"",opts)
通过请求下载数据
下载文件的示例代码:
--[[
xmodem 下载文件
@api downloadFile(url, destination,texpected_md5ype)
@string url http客户端请求地址
@string destination 文件路径
@string expected_md5 MD5校验
@return bool 结果
@usage
downloadFile("http://www.bai.com","/luadb/test.bin")
]]
-- -- 下载文件并验证MD5
local CONFIG = {destination = "/data.bin",timeout = 30000, -- 超时时间(毫秒)max_retries = 3, -- 最大重试次数retry_delay = 1000, -- 重试间隔时间(毫秒)
}
local function downloadFile(url, destination, expected_md5)local req_headers = {['Content-Type'] = 'application/octet-stream',['Accept'] = 'application/octet-stream'}for attempt = 1, CONFIG.max_retries dolog.info(string.format("下载尝试 %d/%d: %s", attempt, CONFIG.max_retries, url))local opts = {dst = destination,timeout = CONFIG.timeout}local response = http.request("GET", url, req_headers, "", opts)if not response thenlog.error("HTTP 请求失败")elselocal code, headers, body = response.wait()log.info("HTTP 响应", "状态码:", code)if code == 200 thenlog.info("文件成功下载到:", destination)-- 打开并读取下载的文件local f = io.open(destination, "rb")log.info("打开的文件",f)if not f thenlog.error("无法打开文件:", destination)elselocal data = f:read("*a")f:close()if not data thenlog.error("文件读取失败:", destination)else-- 验证文件内容local crc = crypto.md5(data)log.info("MD5 校验", crc)if crc == expected_md5 thenlog.info("MD5 校验通过")sys.publish("download_success", true)http_download.download_request = 1return trueelselog.error("MD5 校验未通过")sys.publish("download_success", false)http_download.download_request = 0return falseendendendelselog.error("HTTP 请求失败,状态码:", code)endend-- 如果下载失败,等待后重试if attempt < CONFIG.max_retries thenlog.warn(string.format("下载失败,%d毫秒后重试...", CONFIG.retry_delay))sys.wait(CONFIG.retry_delay)endend-- 所有重试尝试均失败log.error("所有下载尝试均失败")sys.publish("download_success", false)return false
end-- 下载任务初始化
sys.taskInit(function()sys.wait(500)sys.waitUntil("IP_READY") -- 等待网络连接log.info("网络已就绪,开始等待下载指令...")while true dolocal _, data = sys.waitUntil("download")if data thenlocal downloadUrl = data:sub(5, -35)local expected_md5 = data:sub(-32, -1)local destination = CONFIG.destinationlog.info("收到下载指令:", string.fromHex(downloadUrl), "预期MD5:", expected_md5)if downloadUrl and #downloadUrl > 0 thenlocal decoded_url = string.fromHex(downloadUrl)if decoded_url and #decoded_url > 0 thendownloadFile(decoded_url, destination, expected_md5)elselog.error("下载URL解码失败或为空")sys.publish("download_success", false)endelselog.warn("下载URL无效或为空")sys.publish("download_success", false)endelselog.warn("未收到有效的下载指令")sys.wait(600000) -- 休眠10分钟后继续循环endend
end)
下载成功的示例: