我在 VBA (Excel) 中编写了一个函数来处理 API 请求。对于小型(<150MB 左右)请求,它工作得很好,但对于大型请求(>150 MB 到 300-400MB 的返回数据),我得到:
错误:2147024882 (8007000e) 没有足够的存储空间来完成此操作。
以下是代码(删除了不适用的部分):
Function SendHTTPRequest(sURL As String, Optional sGETorPOST As String = "POST", Optional sHeaders2DArray As Variant, Optional sGetBodyOrPostArgs As String) As msxml2.ServerXMLHTTP60
Dim aheaders() As Variant, i As Long
If sGETorPOST <> "GET" And sGETorPOST <> "POST" Then Exit Function
If IsMissing(sHeaders2DArray) Then
ReDim aheaders(0 To 0, 0 To 1)
aheaders(0, 0) = "User-Agent"
aheaders(0, 1) = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
Else
ReDim aheaders(LBound(sHeaders2DArray) To UBound(sHeaders2DArray), LBound(sHeaders2DArray, 2) To UBound(sHeaders2DArray, 2))
aheaders = sHeaders2DArray
End If
Set SendHTTPRequest = New msxml2.ServerXMLHTTP60
SendHTTPRequest.Open sGETorPOST, sURL, False
For i = LBound(aheaders) To UBound(aheaders)
SendHTTPRequest.setRequestHeader aheaders(i, 0), aheaders(i, 1)
Next
If sGETorPOST = "POST" Then SendHTTPRequest.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
SendHTTPRequest.send (sGetBodyOrPostArgs) '<-- Error occurs here
End Function
我已经解决这个问题两天了。我最初是在错误地执行 POST 请求并修复它,然后从 XMLHTTP 更改为 ServerXMLHTTP。
这是调用该函数的适用代码:
aheaders(0, 0) = "User-Agent"
aheaders(0, 1) = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
aheaders(1, 0) = "X-Requested-With"
aheaders(1, 1) = "[redacted]"
aheaders(2, 0) = "Authorization"
aheaders(2, 1) = "Basic " & EncodeBase64(USER & ":" & PASS)
sURL = URLAPI & "/api/2.0/fo/scan/"
sArgs = "action=fetch&scan_ref=" & sRef & "&mode=extended&output_format=json"
Set httpRequest = SendHTTPRequest(sURL, "POST", aheaders, sArgs)
我一直在 Excel 2010、2013 和 2016 中进行测试。这绝对不是资源问题。我正在运行具有 12GB 内存和 500GB SSD 的 Xeon W3550,它的容量还不到一半。
我可以做些什么来修复此代码,还是有另一种方法来发出 API 请求?