同步操作将从 Gitee 极速下载/cf 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
local cf = require "cf"local httpc = require "httpc"local http = require "httpc.class"local json = require "json"local system = require "system"local now = system.nowrequire "utils"-- 这里列举出了httpc库在各种情况下的使用方式.local domain = 'http://localhost:8080' -- domain-- local domain = 'http://127.0.0.1:8080' -- ipv4-- local domain = 'http://[::1]:8080' -- ipv6-- local domain = 'http://[fe80::875:95ce:bcaa:f66%en0]:8080' -- internal ipv6cf.timeout(3, function ( ... )cf.fork(function ( ... )local t1 = now()print("开始时间:", t1)-- GET请求: 在参数固定的情况下可以直接写在url内local code, body = httpc.get(domain.."/api?page=1&limit=10", {{"Auth", "admin"}})print(code, body)-- GET请求: 在参数为动态的情况下可以提供请求数组由httpc库进行拼接local code, body = httpc.get(domain.."/api", {{"Auth", "admin"}}, {{'page', 1}, {'limit', 10}})print(code, body)-- POST HEADER 为数组, BODY为数组local code, body = httpc.post(domain.."/api", {{"Auth", "admin"}}, {{'page', 1}, {'limit', 10}})print(code, body)-- POST HEADER 为数组, BODY为字符串local code, body = httpc.post(domain.."/api", {{"Auth", "admin"}}, "page=1&limit=10")print(code, body)-- http json请求示例local code, body = httpc.json(domain.."/api", {{"Auth", "admin"}}, json.encode({page=1, limit=10}))print(code, body)-- http 上传文件示例local code, body = httpc.file(domain..'/api', nil, {{name='1', filename='1.jpg', file='1', type='jpg'},{name='2', filename='2.jpg', file='2', type='jpg'},})print(code, body)local t2 = now()print("结束时间:", t1, "总耗时:", t2 - t1)end)end)cf.timeout(1, function ( ... )cf.fork(function ( ... )local hc = http:new {}local t1 = now()print("开始时间:", t1)-- GET请求: 在参数固定的情况下可以直接写在url内local code, body = hc:get(domain.."/api?page=1&limit=10", {{"Auth", "admin"}})print(code, body)-- GET请求: 在参数为动态的情况下可以提供请求数组由httpc库进行拼接local code, body = hc:get(domain.."/api", {{"Auth", "admin"}}, {{'page', 1}, {'limit', 10}})print(code, body)-- POST HEADER 为数组, BODY为数组local code, body = hc:post(domain.."/api", {{"Auth", "admin"}}, {{'page', 1}, {'limit', 10}})print(code, body)-- POST HEADER 为数组, BODY为字符串local code, body = hc:post(domain.."/api", {{"Auth", "admin"}}, "page=1&limit=10")print(code, body)-- http json请求示例local code, body = hc:json(domain.."/api", {{"Auth", "admin"}}, json.encode({page=1, limit=10}))print(code, body)local t2 = now()print("结束时间:", t1, "总耗时:", t2 - t1)hc:close()end)end)cf.timeout(5, function ()cf.fork(function ( ... )local hc = http:new {}local t1 = now()print("开始时间:", t1)local ok, response = hc:multi_request {{domain = domain.."/api",method = "get",headers = {{"Auth", "admin"}},args = {{'page', 1}, {'limit', 10}}},{domain = domain.."/api",method = "post",headers = {{"Auth", "admin"}},body = {{'page', 1}, {'limit', 10}}},{domain = domain.."/api",method = "json",headers = {{"Auth", "admin"}},json = json.encode({page=1, limit=10})},{domain = domain.."/api",method = "file",headers = {{"Auth", "admin"}},files = {{name='1', filename='1.jpg', file='1', type='jpg'},{name='2', filename='2.jpg', file='2', type='jpg'},}}}local t2 = now()print("结束时间:", t2, "总耗时:", t2 - t1)require('logging'):new({path = 'test_httpc', dump=true}):DEBUG(response, "回应数量: " .. #response)hc:close()end)end)-- -- 如果有需要可以开启这段注释-- local httpd = require "httpd"-- local app = httpd:new()-- app:api('/api', function (content)-- return "{}"-- end)-- app:listen("", 8080)-- app:run()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。