2016-06-08 2 views
-1

Я использую метод ниже, чтобы сделать HTTP-запрос в моем макросе VBA (скопированный с одного скопированных сайтов).VBA macro 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, и что нужно модифицированном увеличить это время ожидания

ответ

0

Для Q1, подробности вы можете прочитать Microsoft tutorial

Метод SetTimeouts указывает отдельные компоненты тайм-аута операции отправки/получения в миллисекундах.

Метод WaitForResponse ожидает, что асинхронный метод отправки завершится с дополнительным значением тайм-аута в секундах.

Для Q2 это зависит от многих вещей, таких как сеть вашего компьютера и сеть веб-сайта.