我在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内存的Xeon W3550和一款不到半满的500GB SSD。
我可以采取哪些措施来修复此代码,还是有其他方式来发出API请求?