2017-05-11 129 views
0

有人可以幫助我如何從VB6撥打WebApi電話。我發現很少,但沒有任何幫助。以下是URL https://samplewebsiteurl/EnterpriseParticipant,這是期待它撥打電話從VB6到WEBAPI

{ 
"Client": "YYYA", 
"Platforms" : [], 
"ProgramIdentifier": "", 
"MapToEnterpriseView": "true", 
"ParticipantRequest": { 
    "FirstName" : "", 
    "LastName": "Test" 
} 
} 

而且下面是VB6的代碼,我發現該請求。我應該如何通過VB6傳遞請求來調用WebApi?任何我需要使用的參考?

Dim WinHttpReq As Object, status As String, response As String 
On Error GoTo errorfound 
Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1" ;) 
WinHttpReq.open "POST", url, False 
WinHttpReq.send 
+0

你 「發現」 的代碼看起來笨拙轉換的VBScript。但是(a)你沒有發送你的JSON,並且(b。)POST通常需要'application/x-www-form-urlencoded'來實現Content-Type,這意味着你必須首先以有序的方式格式化有效載荷。爲此,您需要知道服務器期望的元素名稱,執行UrlEncoding等。 – Bob77

+0

當然,服務器也可能接受原始JSON,如果它將採用「application/json」而不是。 – Bob77

+0

@ Bob77 我發現很少,我是新來VB6 集myMSXML =新MSXML.XMLHTTPRequest myMSXML.open 「POST」,URL,真 myMSXML.setRequestHeader 「內容類型」,「應用程序/ x-WWW - 窗體-urlencoded」 myMSXML.setRequestHeader‘用戶代理’,‘火狐3.6.4’ myMSXML.OnReadyStateChange =(如下圖所示) myMSXML.send YourPostDataString –

回答

0

這可能是最簡單的例子:

Option Explicit 

'Reference to: 
' 
' Microsoft WinHTTP Services, version 5.1 

Private Req As WinHttp.WinHttpRequest 

Private Sub Command1_Click() 
    With Req 
     .Open "POST", "http://localhost:8080/SomeAPI", Async:=False 
     .SetRequestHeader "Content-Type", "application/hal+json" 
     .SetRequestHeader "Accept", "text/*, application/hal+json, application/json" 
     'Note: Normally you don't include all of this whitespace, but 
     'we'll use it in this example: 
     .Send "{" & vbCrLf _ 
      & """Client"": ""YYYA""," & vbCrLf _ 
      & """Platforms"": []," & vbCrLf _ 
      & """ProgramIdentifier"": """"," & vbCrLf _ 
      & """MapToEnterpriseView"": ""true""," & vbCrLf _ 
      & """ParticipantRequest"": {" & vbCrLf _ 
      & " ""FirstName"" : """"," & vbCrLf _ 
      & " ""LastName"": ""Test""" & vbCrLf _ 
      & "}" & vbCrLf _ 
      & "}" 
     Label1.Caption = CStr(.Status) & " " & .StatusText & vbNewLine _ 
         & .GetAllResponseHeaders() & vbNewLine _ 
         & String$(40, "-") & vbNewLine _ 
         & .ResponseText 
    End With 
End Sub 

Private Sub Form_Load() 
    Set Req = New WinHttp.WinHttpRequest 
End Sub 
+0

工作,現在我正在尋找Json to Object轉換器。你有什麼主意嗎? –