testHttp.lua 5.4 KB

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