从VBA Excel for Mac发送HTTP Post中的数据

所以我有一个使用VBA的Mac版Excel的任务:

在按下button时,我需要从Excel中的工作表中读取数据,parsing这些值并将其导出到Web服务器,该服务器读取数据并将其写入服务器上的文件中。 在Windows上,一切顺利,使用MSXML2.ServerXMLHTTP和所有,但在Mac上是不可能的(这种types的对象是ActiveX的一部分)。

Set objHttp = CreateObject("MSXML2.ServerXMLHTTP") objHttp.SetOption 2, objHttp.GetOption(2) - SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS objHttp.Open "POST", myURL, False objHttp.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" objHttp.SetRequestHeader "Content-type", "application/x-www-form-urlencoded" objHttp.SetRequestHeader "Authorization", "Basic " & Base64Encode(Username & ":" & Password) objHttp.Send (strOutput) 

从这个线程: 如何使用VBA从Excel发送HTTP POST请求到服务器? ,我尝试使用查询表没有任何成功(我总是得到“服务器找不到”的错误,即使我三倍检查地址和所有。)

 With ActiveSheet.QueryTables.Add(Connection:="URL;https://myurl.com/index.php", Destination:=Range("K1")) .PostText = strOutput .RefreshStyle = xlOverwriteCells .SaveData = True .Refresh End With 

我也尝试通过curl命令发送数据,但我似乎无法find如何从VBA中正确执行它。

  sCmd = "curl " & _ "-u " & Username & ":" & Password & _ " -d " & Chr(34) & data & Chr(34) & _ " " & url 

所以如果有人对我应该如何做这个任务有一个想法,我会很感激。 非常感谢。

编辑:添加了我失败的尝试

好的,所以我做了进一步的研究,最终我发现了一些可以使用cURL的东西。 为了做到这一点,我们必须做出这样的function(这是我在这里find的function,在Office 2011 for Mac中的VBA Shellfunction ):

 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 

基本上,这个函数允许我们在Mac上调用shell命令,而不必经历将Mac命令转换为Windows格式的所有困难。

一旦完成,使用第一个参数作为命令行(以cURL格式)调用此函数,第二个参考系统返回的退出代码(而不是cURL请求!)。 cURL命令将返回http响应。 所以这样的事情会起作用:

 Dim command As String command = "usr/bin/curl http://www.myserver.com -d "data" -X POST ..." Dim exitCode As Long Dim result As String result = execShell(command, exitCode) 

注意我如何开始我的cURL请求。 你必须写cURL的path(在本例中为“usr / bin / curl”),以便系统查找cURL的位置,否则它将不起作用(可能会起作用,但是这样我们可以确保它总是会工作)

跨平台兼容性

请注意,如果这个解决scheme在Windows上,这个解决scheme将不起作用,因为它没有“libc.dylib”库,所以你必须检查操作系统并在Windows上执行一个方法在问题中)

 Set objHttp = CreateObject("MSXML2.ServerXMLHTTP") objHttp.SetOption 2, objHttp.GetOption(2) - SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS objHttp.Open "POST", myURL, False objHttp.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" objHttp.SetRequestHeader "Content-type", "application/x-www-form-urlencoded" objHttp.SetRequestHeader "Authorization", "Basic " & Base64Encode(Username & ":" & Password) objHttp.Send (strOutput) 

对于比Mac Excel 2011更高的版本,我推荐这个。

假设我们通过POST操作调用“ http://example.com?q=hello&t=now ”。

 Private Declare PtrSafe Function system Lib "libc.dylib" (ByVal command As String) As Long Function getHTTPPost(sUrl As String, sQuery As String) As Long sCmd = "curl -v -X POST -d '" & sQuery & "' " & sUrl exitCode = system(sCmd) End Sub 

那么,我们可以使用如下:

 ans = getHTTPPost("http://example.com", "q=hello&t=now") 

但是,我们想要考虑的是,我们无法通过此代码获取响应string。

只是希望这可以帮助您关注Office 2016的Mac VBA中的HTTP Post Request。