在XP 2003下的Excel 2003中使用WinHttpRequest

时间:2016-10-03 06:39:13

标签: excel vba winhttprequest

我使用以下代码对HTTP + JSON Web服务执行POST和GET请求。一切都在Win7和Excel 2013下完美运行。在Windows XP和Excel 2003下进行测试时,GET工作正常(请求正文包含JSON对象),但POST没有。应通过线路发送的JSON数据看起来很完美,但在服务器端没有收到,客户端代码也没有报告错误。不幸的是,我无法安装嗅探器以查看真正发送的内容。修改代码以使用MSXML2.ServerXMLHTTP对象后,它工作正常(这将是解决方案)。有没有人有这个问题的经验?我试图找出为什么它不能与WinHttpRequest合作...

'Copied from https://coderwall.com/p/pbxsyw/vba-web-requests
Private Function MakeWebRequest(Method As String, Url As String, PostData As String) As Boolean
    ' make sure to include the Microsoft WinHTTP Services in the project
    ' tools -> references -> Microsoft WinHTTP Services, version 5.1
    ' http://www.808.dk/?code-simplewinhttprequest
    ' http://msdn.microsoft.com/en-us/library/windows/desktop/aa384106(v=vs.85).aspx
    ' http://www.neilstuff.com/winhttp/

    On Error GoTo ErrorHandler:

    ' create the request object
    Set mobjWebReq = CreateObject("WinHttp.WinHttpRequest.5.1")

    ' set timeouts
    ' http://msdn.microsoft.com/en-us/library/windows/desktop/aa384061(v=vs.85).aspx
    ' SetTimeouts(resolveTimeout, ConnectTimeout, SendTimeout, ReceiveTimeout)
    mobjWebReq.SetTimeouts 60000, 60000, 60000, 60000

    If Not LastUsedUrlMonitor Is Nothing Then
        LastUsedUrlMonitor.Value2 = Url
    End If

    ' make the request, http verb (method), url, false to force syncronous
    ' open(http method, absolute uri to request, async (true: async, false: sync)
    mobjWebReq.Open Method, Url, False

    ' handle post content type
    If Method = "POST" Then
        mobjWebReq.SetRequestHeader "Content-type", _
          "application/json"
        If Not LastHttpBodySendMonitor Is Nothing Then
            LastHttpBodySendMonitor.Value2 = PostData
        End If
    End If

    ' set WinHttpRequestOption enumerations
    ' http://msdn.microsoft.com/en-us/library/windows/desktop/aa384108(v=vs.85).aspx

    ' set ssl ignore errors
    '   13056: ignore errors
    '   0: break on errors
    mobjWebReq.Option(4) = 13056

    ' set redirects
    mobjWebReq.Option(6) = True

    ' allow http to redirect to https
    mobjWebReq.Option(12) = True

    ' send request
    ' send post data, should be blank for a get request
    mobjWebReq.Send PostData

    MakeWebRequest = True

    If Not LastHttpBodyReceivedMonitor Is Nothing Then
        LastHttpBodyReceivedMonitor.Value2 = mobjWebReq.Status & ": " & mobjWebReq.StatusText & ", Body: " & mobjWebReq.ResponseText
    End If

    Exit Function

ErrorHandler:
    Select Case Err.Number
        Case &H80072EFD
            MsgBox "Connection to URL: " & Chr(13) & Url & Chr(13) & "failed: " & Err.Description
        Case Else
            MsgBox Err.Number & Chr(13) & Err.Description
    End Select
    Err.Clear
    'Set mobjWebReq = Nothing
    MakeWebRequest = False
    Exit Function
End Function

1 个答案:

答案 0 :(得分:0)

感谢Axel的回复。我刚解决了这个问题(拔掉头发后)。 显然这在Excel 2003中按预期工作:

mobjWebReq.Send (PostData)

这并不是(当 在Excel 2013中工作时)

mobjWebReq.Send PostData

在第二种情况下看起来只是忽略了PostData,并且该问题与对象类型无关。巧合的是,当我使用ServerXMLHTTP对象时,我也使用了括号。