要使用标准的Lua API来获取URL,你需要使用LuaSocket库和LuaSec库
local http = require('socket.http')
local https = require('ssl.https') -- 如果你要获取HTTPS的URL,请使用这个
-- 获取HTTP URL
http.get('http://httpbin.org/get')
-- 获取HTTPS URL
https.get('https://httpbin.org/get')
-- 如果你想处理响应
local response, status, headers, statusline = http.get('http://httpbin.org/get')
if status == 200 then
print('Response:')
print(response)
else
print('Error: '..statusline)
end
如果你要获取HTTPS的URL,只需将'http'替换为'https'即可。请注意,如果你使用LuaSec库,你可能需要自己安装它。
另外,如果你的环境中已经安装了LuaRocks,你可以使用LuaRocks来安装这些库:
luarocks install luasocket
luarocks install luaSec
如果你使用的是Lua 5.1,LuaSocket和LuaSec可能不支持TLS 1.2及更高版本,你需要升级到更高版本的Lua或者使用其他支持TLS 1.2的库,如LuaSec的替代品LuaSocketTLS。
领取专属 10元无门槛券
手把手带您无忧上云