Using MSXML2.ServerXMLHTTP to access data from a web page returns truncated data in Lua

…衆ロ難τιáo~ 提交于 2019-12-13 05:28:29

问题


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.



来源:https://stackoverflow.com/questions/10451064/using-msxml2-serverxmlhttp-to-access-data-from-a-web-page-returns-truncated-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!