VBA macro HTTP request



>我正在使用以下方法在我的 VBA 宏中发出 HTTP 请求(从一个宏复制复制的网站)。

Function GetURLStatus(ByVal URL As String, Optional AllowRedirects As Boolean)
    Const WinHttpRequestOption_EnableRedirects = 6

    If httpRequest Is Nothing Then
        On Error Resume Next
        Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
           If httpRequest Is Nothing Then
              Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5")
           End If
        err.Clear
        On Error GoTo 0
    End If
   ' Control if the URL being queried is allowed to redirect.
   httpRequest.Option(WinHttpRequestOption_EnableRedirects) = AllowRedirects       
  ' Launch the HTTP httpRequest synchronously
  On Error Resume Next
  httpRequest.Open "GET", URL + "x", False
  If err.Number <> 0 Then
    MsgBox err.Number
    ' Handle connection errors
    GetURLStatus = err.Description
    err.Clear
    Exit Function
 End If
 On Error GoTo 0
' Send the http httpRequest for server status
On Error Resume Next
httpRequest.SetTimeouts
httpRequest.Send
httpRequest.WaitForResponse
If err.Number <> 0 Then
    ' Handle server errors
    'PageSource = "Error"
    GetURLStatus = err.Description
    err.Clear
Else
    ' Show HTTP response info
    GetURLStatus = httpRequest.Status & " - " & httpRequest.StatusText
    ' Save the web page text
    'PageSource = httpRequest.ResponseText
End If
 On Error GoTo 0
 End Function
  1. 有人可以解释一下httpRequest.SetTimeouts的目的是什么吗?httpRequest.WaitForResponse.

  2. 默认情况下,它将等待 HTTP 响应多长时间,以及需要什么修改以增加此等待时间

对于第 1 季度,您可以在教程Microsoft阅读的详细信息

方法指定发送/接收操作的各个超时组件(以毫秒为单位)。

方法等待异步发送方法完成,具有可选的超时值(以秒为单位)。

对于Q2,它取决于许多因素,例如计算机的网络和网站的网络。

最新更新