Excel HTTP获取超时

时间:2018-04-13 23:06:07

标签: excel excel-vba vba

我们有这个小子,它只是将数据泵入远程服务器。一切都按预期工作。

前几天,网络服务器上发生了一个持续了大约一个小时的事件。我仍然可以PING服务器,但IIS没有响应。结果,宏只是等待响应。

有关快速通过/失败测试或超时的任何想法?

Sub WebLog(getUser As String, getEvent As String, getValue As String)
    Dim URL As String
    URL = "http://myurl.com/dp.aspx?Task=Log&v1=" + getUser + "&v2=" + getEvent + "&v3=" + getValue
    Dim xml As Object
    Set xml = CreateObject("MSXML2.XMLHTTP")
    xml.Open "GET", URL, False
    xml.Send
End Sub

1 个答案:

答案 0 :(得分:8)

您可能需要查看WinHttp.WinHttpRequest.5.1库,该库可让您为请求设置timeouts

  

WinHttpRequest object

您应该在.SetTimeouts之前设置.Open,并且为.SetTimeouts设置的四个参数是(以毫秒为单位):

Resolve, Connect, Send and Receive

您的示例代码将是(10秒超时):

Sub WebLog(getUser As String, getEvent As String, getValue As String)
    Dim URL As String
    URL = "http://myurl.com/dp.aspx?Task=Log&v1=" + getUser + "&v2=" + getEvent + "&v3=" + getValue
    Dim xml As Object
    Set xml = CreateObject("WinHttp.WinHttpRequest.5.1")
    xml.SetTimeouts 10000, 10000, 10000, 10000
    xml.Open "GET", URL, False
    xml.Send
End Sub