XMLHTTP onTimeOut 时如何使用 VBA 回调函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11407010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:49:32  来源:igfitidea点击:

How to VBA callback function when XMLHTTP onTimeOut?

excelvbacallbacktimeoutmsxml

提问by Davuz

I'm trying get xml data from webserver to excel, then I wrote a sendRequestfunction to call in excel

我正在尝试从网络服务器获取 xml 数据到 excel,然后我编写了一个sendRequest函数来调用 excel

=sendRequest("http://abb.com/index.php?id=111")

=sendRequest("http://abb.com/index.php?id=111")

When web-server having trouble, cannot connect or cannot find, excel is not responding, it was horrible! To avoid it, i think we should set timeOut. These are my function:

当网络服务器出现问题、无法连接或找不到时,excel 没有响应,太可怕了!为了避免它,我认为我们应该设置超时。这些是我的功能:

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

Normally, when XMLHTTP's timeout occurs, event OnTimeOutMessagewill be executed (reference #1, #2). But as in my test, OnTimeOutMessageis executed right at the beginning of sendRequest()

通常,当XMLHTTP的超时发生时,事件OnTimeOutMessage将被执行(参考#1, #2)。但是在我的测试中OnTimeOutMessage在开始时执行sendRequest()

How to use callback function when Msxml2.ServerXMLHTTP.6.0request is time-out?

Msxml2.ServerXMLHTTP.6.0请求超时时如何使用回调函数?

Thank for your help!

感谢您的帮助!

采纳答案by Alex K.

The line;

线;

XMLHTTP.OnTimeOut = OnTimeOutMessage

XMLHTTP.OnTimeOut = OnTimeOutMessage

Is not a method assignment; rather it immediately executes OnTimeOutMessage()(and assigns its useless return value to OnTimeOut).

不是方法赋值;而是立即执行OnTimeOutMessage()(并将其无用的返回值分配给OnTimeOut)。

The equivalent line in JavaScriptas per your example link correctly assigns a Functionobject to OnTimeOutfor subsequent invokation - this is not supported by VBA.

根据您的示例链接,JavaScript 中的等效行正确FunctionOnTimeOut为后续调用分配了一个对象- VBA 不支持这一点。

Instead, you could trap the timeout error raised after .sendor use early binding, WithEvents, & inline event handlers.

相反,您可以捕获在.send或使用早期绑定、WithEvents、 和内联事件处理程序之后引发的超时错误。

回答by osknows

Tim is correct in that if you have timeouts then each request will 'hang' Excel/VBA until the timeout duration has elapsed before continuing. Using async will allow you multiple requests without a long request delaying either a response or further requests.

蒂姆是正确的,因为如果您有超时,那么每个请求都会“挂起”Excel/VBA,直到超时持续时间过去,然后才能继续。使用 async 将允许您进行多个请求,而不会延迟响应或进一步请求的长请求。

The status property represents the HTTP status code returned by a request. Just place the code below in your existing code for a slower synchronous check or move your response processing to an event handler for async.

status 属性表示请求返回的 HTTP 状态代码。只需将下面的代码放在您现有的代码中以进行较慢的同步检查或将您的响应处理移动到异步事件处理程序。

XMLHTTP.Send

If XMLHTTP.Status = "200" Then
    '200      OK
    htmlString = XMLHTTP.ResponseText
Elseif XMLHTTP.Status = "408" Then
    '408      Request Timeout
    Call OnTimeOutMessage
else
    'All status return values
    'Number      Description
    '100      Continue
    '101      Switching protocols
    '200      OK
    '201      Created
    '202      Accepted
    '203      Non-Authoritative Information
    '204      No Content
    '205      Reset Content
    '206      Partial Content
    '300      Multiple Choices
    '301      Moved Permanently
    '302      Found
    '303      See Other
    '304      Not Modified
    '305      Use Proxy
    '307      Temporary Redirect
    '400      Bad Request
    '401      Unauthorized
    '402      Payment Required
    '403      Forbidden
    '404      Not Found
    '405      Method Not Allowed
    '406      Not Acceptable
    '407      Proxy Authentication Required
    '408      Request Timeout
    '409      Conflict
    '410      Gone
    '411      Length Required
    '412      Precondition Failed
    '413      Request Entity Too Large
    '414      Request-URI Too Long
    '415      Unsupported Media Type
    '416      Requested Range Not Suitable
    '417      Expectation Failed
    '500      Internal Server Error
    '501      Not Implemented
    '502      Bad Gateway
    '503      Service Unavailable
    '504      Gateway Timeout
    '505      HTTP Version Not Supported

End If