Macos OSX Excel中使用VBA的HTTP Get请求

Macos OSX Excel中使用VBA的HTTP Get请求,macos,vba,excel,nginx,Macos,Vba,Excel,Nginx,我正在编写一个非常简单的宏,它需要向服务器发出HTTP GET请求,而响应并不重要(它会在服务器上启动一个进程)。HTTP GET不需要身份验证 我正在使用以下代码“成功”完成此操作(服务器日志指示向服务器发出的请求,但服务器正在运行HTTP 406): 但我从服务器返回以下响应: Unable to open http://someurl.com Cannot locate the Internet server or proxy server. 我可以看到服务器返回一个HTTP 406,在

我正在编写一个非常简单的宏,它需要向服务器发出HTTP GET请求,而响应并不重要(它会在服务器上启动一个进程)。HTTP GET不需要身份验证

我正在使用以下代码“成功”完成此操作(服务器日志指示向服务器发出的请求,但服务器正在运行HTTP 406):

但我从服务器返回以下响应:

Unable to open http://someurl.com Cannot locate the Internet server or proxy server.
我可以看到服务器返回一个HTTP 406,在进行一些研究之后,这是因为GET请求没有发送正确的
内容类型


所以我的问题是-如何告诉
ActiveSheet.QueryTables.Add
设置标题,或者如何修改我的NGINX配置以支持这个特定的GET调用

下面是一段同时支持OSX和Windows的代码。我相信这篇文章的作者,因为我肯定不是白手起家写的,但我已经不知道这一切从何而来:

#If Win32 Then

Function getHTTP(URL As String, sQuery As String) As String
    Dim strResult As String
    Dim objHTTP As Object
    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    objHTTP.Open "GET", URL & "?" & sQuery, False
    objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    objHTTP.send("")
    strResult = objHTTP.responseText
    getHTTP = strResult
End Function


#Else


Option Explicit
' execShell() function courtesy of Robert Knight via StackOverflow
' http://stackoverflow.com/questions/6136798/vba-shell-function-in-office-2011-for-mac
Private Declare Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As Long
Private Declare Function pclose Lib "libc.dylib" (ByVal file As Long) As Long
Private Declare Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As Long, ByVal items As Long, ByVal stream As Long) As Long
Private Declare Function feof Lib "libc.dylib" (ByVal file As Long) As Long


Function execShell(command As String, Optional ByRef exitCode As Long) As String
Dim file As Long
file = popen(command, "r")
If file = 0 Then
Exit Function
End If
While feof(file) = 0
Dim chunk As String
Dim read As Long
chunk = Space(50)
read = fread(chunk, 1, Len(chunk) - 1, file)
If read > 0 Then
chunk = Left$(chunk, read)
execShell = execShell & chunk
End If
Wend
exitCode = pclose(file)
End Function


Function getHTTP(sUrl As String, sQuery As String) As String
    Dim sCmd As String
    Dim sResult As String
    Dim lExitCode As Long
    sCmd = "curl --get -d """ & sQuery & """" & " " & sUrl
    sResult = execShell(sCmd, lExitCode)
    ' ToDo check lExitCode
    getHTTP = sResult
End Function

#End If

这对我在MacExcel15.4上也不起作用。我在VBA Tools Github上找到了一个有用的版本:

以下是对我有效的版本:

Private Declare PtrSafe Function web_popen Lib "libc.dylib" Alias "popen" (ByVal command As String, ByVal mode As String) As LongPtr
Private Declare PtrSafe Function web_pclose Lib "libc.dylib" Alias "pclose" (ByVal file As LongPtr) As Long
Private Declare PtrSafe Function web_fread Lib "libc.dylib" Alias "fread" (ByVal outStr As String, ByVal size As LongPtr, ByVal items As LongPtr, ByVal stream As LongPtr) As Long
Private Declare PtrSafe Function web_feof Lib "libc.dylib" Alias "feof" (ByVal file As LongPtr) As LongPtr

Public Function executeInShell(web_Command As String) As String

    Dim web_File As LongPtr
    Dim web_Chunk As String
    Dim web_Read As Long

    On Error GoTo web_Cleanup

    web_File = web_popen(web_Command, "r")

    If web_File = 0 Then
        Exit Function
    End If

    Do While web_feof(web_File) = 0
        web_Chunk = VBA.Space$(50)
        web_Read = web_fread(web_Chunk, 1, Len(web_Chunk) - 1, web_File)
        If web_Read > 0 Then
            web_Chunk = VBA.Left$(web_Chunk, web_Read)
            executeInShell = executeInShell & web_Chunk
        End If
    Loop

web_Cleanup:

    web_pclose (web_File)

End Function


Function getHTTP(sUrl As String, sQuery As String) As String
    Dim sCmd As String
    Dim sResult As String
    Dim lExitCode As Long
    sCmd = "curl --get -d """ & sQuery & """" & " " & sUrl
    sResult = executeInShell(sCmd)
    getHTTP = sResult
End Function

也许可以检查一下这一点,它基本上是说使用shell来cURL,而不是使用
QueryTables
方法来进行HTTP GET。您可以这样设置请求头。看起来不错。但在Mac Excel 15上对我不起作用。“私有声明函数popen”这些行是红色的?!是的,最新版本的excel破坏了这一点,我还没有找到修复方法。请使用Private Declare PtrSafe函数。。。这应该是工作!这适用于Windows吗?我正在Mac上开发,但希望它在Windows上兼容。
Private Declare PtrSafe Function web_popen Lib "libc.dylib" Alias "popen" (ByVal command As String, ByVal mode As String) As LongPtr
Private Declare PtrSafe Function web_pclose Lib "libc.dylib" Alias "pclose" (ByVal file As LongPtr) As Long
Private Declare PtrSafe Function web_fread Lib "libc.dylib" Alias "fread" (ByVal outStr As String, ByVal size As LongPtr, ByVal items As LongPtr, ByVal stream As LongPtr) As Long
Private Declare PtrSafe Function web_feof Lib "libc.dylib" Alias "feof" (ByVal file As LongPtr) As LongPtr

Public Function executeInShell(web_Command As String) As String

    Dim web_File As LongPtr
    Dim web_Chunk As String
    Dim web_Read As Long

    On Error GoTo web_Cleanup

    web_File = web_popen(web_Command, "r")

    If web_File = 0 Then
        Exit Function
    End If

    Do While web_feof(web_File) = 0
        web_Chunk = VBA.Space$(50)
        web_Read = web_fread(web_Chunk, 1, Len(web_Chunk) - 1, web_File)
        If web_Read > 0 Then
            web_Chunk = VBA.Left$(web_Chunk, web_Read)
            executeInShell = executeInShell & web_Chunk
        End If
    Loop

web_Cleanup:

    web_pclose (web_File)

End Function


Function getHTTP(sUrl As String, sQuery As String) As String
    Dim sCmd As String
    Dim sResult As String
    Dim lExitCode As Long
    sCmd = "curl --get -d """ & sQuery & """" & " " & sUrl
    sResult = executeInShell(sCmd)
    getHTTP = sResult
End Function