使用 MSXML2.ServerXMLHTTP 從網頁訪問數據會在 Lua 中返回截斷的數據 (Using MSXML2.ServerXMLHTTP to access data from a web page returns truncated data in Lua)


問題描述

使用 MSXML2.ServerXMLHTTP 從網頁訪問數據會在 Lua 中返回截斷的數據 (Using MSXML2.ServerXMLHTTP to access data from a web page returns truncated data in Lua)

I am trying to download a source code file from a web site which works fine for small files,  but a couple of larger ones get truncated.

The example below should be returning a file 146,135 bytes in size, but returns one of 141,194 bytes with a status of 200.

I have tried winhttp.winhttprequest.5.1 as well, but both seem to truncate at the same point.

I have also found quite a few people with similar problems, but have not been able to find a solution.

require('luacom')

http = luacom.CreateObject('MSXML2.ServerXMLHTTP')

http:Open("GET","http://www.family‑historian.co.uk/wp‑content/plugins/forced‑download2/download.php?path=/wp‑content/uploads/formidable/tatewise/&file=Map‑Life‑Facts3.fh_lua&id=190",true)
http:Send()
http:WaitForResponse(30)
print('Status: '..http.Status)
print('‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑')
headers = http:GetAllResponseHeaders()
data = http.Responsetext 
print('Data Size = '..#data)
print('‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑')
print(headers)

‑‑‑‑‑

參考解法

方法 1:

I finally worked out what was going on so will post it here for others.

To avoid the truncation I needed to use ResponseBody and not ResponseText,  what appears to be happening is the file is being sent in binary format, the ResponseText data is the same number of bytes as the ResponseBody one,  but is in UTF‑8 format, this means the number if special characters in the file (which are double byte in UTF‑8 are dropped from the end of the ResponseText. I am not sure at what level the "mistake" in the length is made, but the way to avoid it is to use ResponseBody.

(by Jane TJane T)

參考文件

  1. Using MSXML2.ServerXMLHTTP to access data from a web page returns truncated data in Lua (CC BY‑SA 3.0/4.0)

#lua #msxml






相關問題

使用 MSXML2.ServerXMLHTTP 從網頁訪問數據會在 Lua 中返回截斷的數據 (Using MSXML2.ServerXMLHTTP to access data from a web page returns truncated data in Lua)

如何在 VS 2008 中包含 Lua 庫 (How can I include Lua library in VS 2008)

Corona 中的 iPad 式慣性滾動 (iPad-style inertial scrolling in Corona)

Lua cư xử kỳ lạ trên nền tảng PowerPC / LynxOS, tại sao? (Lua behaves weird on PowerPC/LynxOS platform, why?)

我們如何在函數輸入參數中輸入類型值作為對象? (How do we input type value as object in function input parameter?)

反編譯 Lua 字節碼的最佳工具? (Best tool(s) for decompiling Lua bytecode?)

本機 Lua 中的高效可變字節數組 (Efficient mutable byte array in native Lua)

在 Lua 中字符串化對象名稱 (Stringify object name in Lua)

純 Lua 中的全功能正則表達式庫 (Fully-featured regex library in pure Lua)

自我作為參數,並設置範圍? (self as param, and setting scope?)

已經放 } 但錯誤仍然說 } 是預期的? (Already put } but the error still says that } is expected?)

我想使用 HPC 的 gpu 並嘗試 module add CUDA ...但出現錯誤。錯誤是“Lmod 檢測到以下錯誤: (I want to use the gpu of the HPC and try module add CUDA... But errors occurs. The error is "Lmod has detected the following error:)







留言討論