说明
在lua中使用网络库需要用luasocket
,所以得先下载一个luasocket
库。他的官网:http://files.luaforge.net/releases/luasocket/luasocket
代码
local http = require("socket.http") -- 加载http库
local ltn12 = require("ltn12")
-- 简单的定义个Get方法
function http.get(u)
local t = {}
local r, c, h = http.request{
method = "GET", -- 这里也可以使用POST
url = u,
sink = ltn12.sink.table(t)} -- 链接返回的body
return r, c, h, table.concat(t)
end
url = "http://172.16.40.140:8081/tools/"
r,c,h,body=http.get(url) -- 使用GET方法
if c~= 200 then
return
end
print(body)