testHttp.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. --- 模块功能:HTTP功能测试.
  2. -- @author openLuat
  3. -- @module http.testHttp
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2018.03.23
  7. module(...,package.seeall)
  8. require"http"
  9. local function cbFnc(result,prompt,head,body)
  10. log.info("testHttp.cbFnc",result,prompt)
  11. if result and head then
  12. for k,v in pairs(head) do
  13. log.info("testHttp.cbFnc",k..": "..v)
  14. end
  15. end
  16. if result and body then
  17. log.info("testHttp.cbFnc","bodyLen="..body:len())
  18. end
  19. end
  20. local function cbFncFile(result,prompt,head,filePath)
  21. log.info('看看',filePath)
  22. log.info("testHttp.cbFncFile",result,prompt,filePath)
  23. if result and head then
  24. for k,v in pairs(head) do
  25. log.info("testHttp.cbFncFile",k..": "..v)
  26. end
  27. end
  28. if result and filePath then
  29. local size = io.fileSize(filePath)
  30. log.info("testHttp.cbFncFile","fileSize="..size)
  31. --输出文件内容,如果文件太大,一次性读出文件内容可能会造成内存不足,分次读出可以避免此问题
  32. if size<=4096 then
  33. log.info("testHttp.cbFncFile",io.readFile(filePath))
  34. else
  35. end
  36. end
  37. --文件使用完之后,如果以后不再用到,需要自行删除
  38. if filePath then os.remove(filePath) end
  39. end
  40. sys.taskInit(function ()
  41. while true do
  42. sys.wait(20000)
  43. http.request("GET","http://cdn.openluat-luatcommunity.openluat.com/attachment/20211208190511374_1.zip",nil,nil,nil,30000,cbFncFile,"123.zip")
  44. sys.wait(20000)
  45. --return
  46. end
  47. end)
  48. sys.timerLoopStart(function ()
  49. log.info("打印占用的内存:", _G.collectgarbage("count"))
  50. end,5000)
  51. --http.request("GET","https://www.baidu.com",{caCert="ca.crt"},nil,nil,nil,cbFnc)
  52. --http.request("GET","www.lua.org",nil,nil,nil,30000,cbFncFile,"download.bin")
  53. --http.request("GET","http://www.lua.org",nil,nil,nil,30000,cbFnc)
  54. --http.request("GET","www.lua.org/about.html",nil,nil,nil,30000,cbFnc)
  55. --http.request("GET","www.lua.org:80/about.html",nil,nil,nil,30000,cbFnc)
  56. --http.request("POST","www.iciba.com",nil,nil,"Luat",30000,cbFnc)
  57. --http.request("POST","36.7.87.100:6500",nil,{head1="value1"},{[1]="begin\r\n",[2]={file="/lua/http.lua"},[3]="end\r\n"},30000,cbFnc)
  58. --http.request("POST","http://lq946.ngrok.xiaomiqiu.cn/",nil,nil,{[1]="begin\r\n",[2]={file_base64="/lua/http.lua"},[3]="end\r\n"},30000,cbFnc)
  59. --如下示例代码是利用文件流模式,上传录音文件的demo,使用的URL是随意编造的
  60. --[[
  61. http.request("POST","www.test.com/postTest?imei=1&iccid=2",nil,
  62. {['Content-Type']="application/octet-stream",['Connection']="keep-alive"},
  63. {[1]={['file']="/RecDir/rec001"}},
  64. 30000,cbFnc)
  65. ]]
  66. --如下示例代码是利用x-www-form-urlencoded模式,上传3个参数,通知openluat的sms平台发送短信
  67. --[[
  68. function urlencodeTab(params)
  69. local msg = {}
  70. for k, v in pairs(params) do
  71. table.insert(msg, string.urlEncode(k) .. '=' .. string.urlEncode(v))
  72. table.insert(msg, '&')
  73. end
  74. table.remove(msg)
  75. return table.concat(msg)
  76. end
  77. http.request("POST","http://api.openluat.com/sms/send",nil,
  78. {
  79. ["Authorization]"="Basic jffdsfdsfdsfdsfjakljfdoiuweonlkdsjdsjapodaskdsf",
  80. ["Content-Type"]="application/x-www-form-urlencoded",
  81. },
  82. urlencodeTab({content="您的煤气检测处于报警状态,请及时通风处理!", phone="13512345678", sign="短信发送方"}),
  83. 30000,cbFnc)
  84. ]]
  85. --如下示例代码是利用multipart/form-data模式,上传2参数和1个照片文件
  86. --[[
  87. local function postMultipartFormData(url,cert,params,timeout,cbFnc,rcvFileName)
  88. local boundary,body,k,v,kk,vv = "--------------------------"..os.time()..rtos.tick(),{}
  89. for k,v in pairs(params) do
  90. if k=="texts" then
  91. local bodyText = ""
  92. for kk,vv in pairs(v) do
  93. bodyText = bodyText.."--"..boundary.."\r\nContent-Disposition: form-data; name=\""..kk.."\"\r\n\r\n"..vv.."\r\n"
  94. end
  95. body[#body+1] = bodyText
  96. elseif k=="files" then
  97. local contentType =
  98. {
  99. jpg = "image/jpeg",
  100. jpeg = "image/jpeg",
  101. png = "image/png",
  102. }
  103. for kk,vv in pairs(v) do
  104. print(kk,vv)
  105. body[#body+1] = "--"..boundary.."\r\nContent-Disposition: form-data; name=\""..kk.."\"; filename=\""..kk.."\"\r\nContent-Type: "..contentType[vv:match("%.(%w+)$")].."\r\n\r\n"
  106. body[#body+1] = {file = vv}
  107. body[#body+1] = "\r\n"
  108. end
  109. end
  110. end
  111. body[#body+1] = "--"..boundary.."--\r\n"
  112. http.request(
  113. "POST",
  114. url,
  115. cert,
  116. {
  117. ["Content-Type"] = "multipart/form-data; boundary="..boundary,
  118. ["Connection"] = "keep-alive"
  119. },
  120. body,
  121. timeout,
  122. cbFnc,
  123. rcvFileName
  124. )
  125. end
  126. postMultipartFormData(
  127. "1.202.80.121:4567/api/uploadimage",
  128. nil,
  129. {
  130. texts =
  131. {
  132. ["imei"] = "862991234567890",
  133. ["time"] = "20180802180345"
  134. },
  135. files =
  136. {
  137. ["logo_color.jpg"] = "/ldata/logo_color.jpg"
  138. }
  139. },
  140. 60000,
  141. cbFnc
  142. )
  143. ]]