我正在尝试从网络服务器获取 xml 数据到 excel,然后我编写了一个sendRequest
函数来调用 excel
=sendRequest("http://abb.com/index.php?id=111")
当网络服务器出现问题,无法连接或找不到时,excel 没有响应,太可怕了!为了避免它,我认为我们应该设置timeOut。这些是我的功能:
Function sendRequest(Url)
'Call service
Set XMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.6.0")
'Timeout values are in milli-seconds
lResolve = 10 * 1000
lConnect = 10 * 1000
lSend = 10 * 1000
lReceive = 15 * 1000 'waiting time to receive data from server
XMLHTTP.setTimeOuts lResolve, lConnect, lSend, lReceive
XMLHTTP.OnTimeOut = OnTimeOutMessage 'callback function
XMLHTTP.Open "GET", Url, False
On Error Resume Next
XMLHTTP.Send
On Error GoTo 0
sendRequest = (XMLHTTP.responseText)
End Function
Private Function OnTimeOutMessage()
'Application.Caller.Value = "Server error: request time-out"
MsgBox ("Server error: request time-out")
End Function
通常,当XMLHTTP
' 超时发生时,事件OnTimeOutMessage
将被执行(参考#1,#2)。但在我的测试中,OnTimeOutMessage
是在开始时执行的sendRequest()
Msxml2.ServerXMLHTTP.6.0请求超时时如何使用回调函数?
感谢您的帮助!