2016-07-05 2 views
1

서버에 HTTP GET 요청을해야하고 응답이 중요하지 않은 (서버에서 프로세스를 시작하는) 매우 간단한 매크로를 작성하고 있습니다. HTTP GET에는 인증이 필요하지 않습니다. 나는이 작업을 수행하려면 다음 코드를 사용하고OSX Excel에서 VBA를 사용하여 HTTP 요청 받기

는 "성공적으로"(서버 로그는 요청이 서버에 만든 표시하지만 서버는 HTTP 406 실행) :

Function callAPI(Url As String) 
    With ActiveSheet.QueryTables.Add(Connection:="URL;" & Url, Destination:=Range("D15")) 
    .PostText = "" 
    .RefreshStyle = xlOverwriteCells 
    .SaveData = True 
    .Refresh 
    End With 
End Function 

을하지만 돌아 가야 서버에서 다음과 같은 응답 :

Unable to open http://someurl.com Cannot locate the Internet server or proxy server. 

내가 서버를 볼 수는 GET 요청이 올바른 Content-Type 헤더를 전송하지 않기 때문에 몇 가지 조사가 발생 후, HTTP 406을 반환합니다.

그래서 제 질문은 - 어떻게 헤더를 설정하는 ActiveSheet.QueryTables.Add을 알 수 있습니까, 또는 어떻게이 특정 GET의 CALL 여기

+0

어쩌면이를 확인을 http://stackoverflow.com/questions/15981960/ how-do-i-issue-http-get-from-excel-vba-for-mac-2011은 기본적으로 HTTP GET을 수행하는 방법으로 QueryTables 메소드를 사용하는 대신 cURL을 사용하는 것을 말합니다. 이 방법으로 요청 헤더를 설정할 수 있습니다. –

답변

1

OSX와 Windows를 지원하는 코드 조각이에게 지원하기 위해 내의 nginx의 설정을 수정할 수 없습니다. 나는 확실히 처음부터이 쓰기 didnt가 있기 때문에 나는이의 저자를 신용 것입니다하지만 난 그것을 모두가 어디에서 왔는지 추적 잃었 :이 중 맥 엑셀 15.4에 나를 위해 작동하지 않는

#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 
+1

좋아 보인다. 하지만 맥 엑셀 15에서 나에게 도움이되지 않는다. "Private Declare Function popen"이 줄들은 빨갛다. – fabian

+1

네, 엑셀의 최신 버전이 고장났습니다. – josephmisiti

+1

Private Declare PtrSafe 함수를 사용하십시오 ... 작동해야합니다! – Lin

0

합니다.

여기 https://github.com/VBA-tools/VBA-Web/issues/248

이 나를 위해 일한 버전입니다 : 내가 Github에서 다음 VBA-도구에 도움이 버전의 발견

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 
관련 문제