使用异步XMLHTTP获得部分响应

时间:2013-05-23 14:50:14

标签: http vbscript

如何在VBS完成之前异步读取XMLHTTP?这个例子 下面(其中有“完成此操作所需的数据尚不可用” 错误,如果睡眠时间不够长)使用一个大的维基百科页面,但最终我 我想把它用于永远不会完成加载的无限网页/流。所以, 是否有一种简单的方法来修改此示例以尽早获得部分HTTP响应?

strFileURL = "https://en.wikipedia.org/wiki/Algorithm"  ' or any big webpage

Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")   ' all of these xmlhttp options have no "streaming success"
' Set objXMLHTTP = CreateObject("Microsoft.XMLHTTP")
' Set objXMLHTTP = CreateObject("Microsoft.XmlHttp")
' Set objXMLHTTP = Createobject("MSXML2.XMLHTTP.3.0")
' Set objXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.6.0")

objXMLHTTP.open "GET", strFileURL, true
objXMLHTTP.send()

' Is there something other than ResponseText to avoid error and get partial result below (i.e., read html like a stream)?
WScript.Sleep 500                   ' need to play with this time to try to cause mystate1=3 below
mystate1 = objXMLHTTP.ReadyState    ' should be 3
myscan = objXMLHTTP.ResponseText    ' CAUSES ERROR if mystate1 < 4 (comment this line out to avoid error)
WScript.Sleep 1000
mystate2 = objXMLHTTP.ReadyState    ' should be 4
myscan = objXMLHTTP.ResponseText

MsgBox(mystate1)
MsgBox(mystate2)
MsgBox(myscan)

Set objXMLHTTP = Nothing

1 个答案:

答案 0 :(得分:0)

这是不可能的,至少不是通过使用XMLHTTPRequest。截至documentedresponseTextresponseBody在请求完成之前均无法使用。