XP下Excel2003中WinHttpRequest的使用
Use of WinHttpRequest in Excel 2003 under XP
我正在使用下面的代码对我的 HTTP+JSON 网络服务执行 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
感谢阿克塞尔的回复。我刚刚解决了这个问题(拔掉头发后)。
显然这在 Excel 2003 中按预期工作:
mobjWebReq.Send (PostData)
这并没有(虽然 在 Excel 2013 年有效)
mobjWebReq.Send PostData
在第二种情况下,PostData 似乎被忽略了,问题与对象类型无关。巧合的是,当我使用 ServerXMLHTTP 对象时,我也使用了括号。
我正在使用下面的代码对我的 HTTP+JSON 网络服务执行 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
感谢阿克塞尔的回复。我刚刚解决了这个问题(拔掉头发后)。 显然这在 Excel 2003 中按预期工作:
mobjWebReq.Send (PostData)
这并没有(虽然 在 Excel 2013 年有效)
mobjWebReq.Send PostData
在第二种情况下,PostData 似乎被忽略了,问题与对象类型无关。巧合的是,当我使用 ServerXMLHTTP 对象时,我也使用了括号。