源战役客户端
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

141 linhas
3.5 KiB

  1. HttpUtil = HttpUtil or {}
  2. local HttpUtil = HttpUtil
  3. -- not_need_random: 不需要随机数
  4. function HttpUtil.HttpGetRequest(url, get_data, call_func, timeout, not_need_random)
  5. local request_count = 0
  6. local do_func
  7. do_func = function()
  8. request_count = request_count + 1
  9. timeout = timeout or 5000
  10. get_data = get_data or {}
  11. local function http_back( response )
  12. if response then
  13. local data = response:GetResponseText()
  14. local state_code = response.StatusCode
  15. local error_msg = response.Error
  16. local ret = false
  17. if state_code == 200 then
  18. ret = true
  19. end
  20. -- if not ret and request_count == 1 then
  21. -- if ClientConfig.backurl_account_path and string.find(url, ClientConfig.login_php) then
  22. -- url = string.gsub(ClientConfig.login_php,ClientConfig.url_account_path,ClientConfig.backurl_account_path)
  23. -- setTimeout(do_func,0.1)
  24. -- return
  25. -- end
  26. -- end
  27. if call_func then
  28. call_func(ret,error_msg,data)
  29. end
  30. --EngineDebug.Log("-----HTTPRequestData------" .. (data or "empty"))
  31. end
  32. end
  33. local req_url = url .. "?v=" .. os.time()
  34. for name,value in pairs(get_data) do
  35. req_url = req_url .. "&" .. name .. "=" .. value
  36. end
  37. if not_need_random then --无需随机数
  38. req_url = url
  39. end
  40. EngineDebug.Log("-----SentHTTPRequest------" .. req_url)
  41. local req_obj = httpManager:Request( req_url, "GET", timeout, http_back)
  42. req_obj:Start()
  43. end
  44. setTimeout(do_func,0.1)
  45. end
  46. function HttpUtil.HttpByteBuffSent(url, post_data, file_path, call_func, timeout)
  47. timeout = timeout or 5000
  48. post_data = post_data or {}
  49. local function http_back( response )
  50. if response then
  51. local data = response:GetResponseText()
  52. local state_code = response.StatusCode
  53. local error_msg = response.Error
  54. local ret = false
  55. if state_code == 200 then
  56. ret = true
  57. end
  58. if call_func then
  59. call_func(ret,data)
  60. end
  61. end
  62. end
  63. local req_url = url .. "?v=" .. os.time()
  64. for name,value in pairs(post_data) do
  65. req_url = req_url .. "&" .. name .. "=" .. value
  66. end
  67. print("----HttpByteBuffSent---",req_url)
  68. local req_obj = httpManager:Request( req_url, "BYTEBUFF", timeout, http_back)
  69. req_obj:SetSentFilePath(file_path)
  70. req_obj:Start()
  71. end
  72. function HttpUtil.HttpPostRequest(url, post_data, call_func, timeout)
  73. timeout = timeout or 5000
  74. post_data = post_data or {}
  75. local function http_back( response )
  76. if response then
  77. local data = response:GetResponseText()
  78. local state_code = response.StatusCode
  79. local error_msg = response.Error
  80. local ret = false
  81. if state_code == 200 then
  82. ret = true
  83. end
  84. if call_func then
  85. call_func(ret,error_msg,data)
  86. end
  87. end
  88. end
  89. local req_obj = httpManager:Request( url, "POST", timeout, http_back)
  90. for name,value in pairs(post_data) do
  91. req_obj:AddPostData(name,value)
  92. end
  93. req_obj:Start()
  94. end
  95. -- not_need_random: 不需要随机数
  96. function HttpUtil.HttpDownLoad(url, save_path, call_func, timeout, not_need_random)
  97. timeout = timeout or 5000
  98. local function http_back( response )
  99. if response then
  100. local state_code = response.StatusCode
  101. local error_msg = response.Error
  102. if save_path then
  103. response:SaveResponseToFile(save_path)
  104. end
  105. local ret = false
  106. if state_code == 200 then
  107. ret = true
  108. end
  109. if call_func then
  110. call_func(ret, error_msg)
  111. end
  112. end
  113. end
  114. local req_url = url .. "?v=" .. os.time()
  115. if not_need_random then
  116. req_url = url
  117. end
  118. local req_obj = httpManager:Request( req_url, "Get", timeout, http_back)
  119. req_obj:Start()
  120. end