【问题标题】:Make a call from VB6 to WEBAPI从 VB6 调用 WEBAPI
【发布时间】:2020-09-30 16:06:56
【问题描述】:

谁能帮助我如何从 VB6 进行 WebApi 调用。我发现很少但没有任何帮助。以下是Url https://samplewebsiteurl/EnterpriseParticipant,这是它期望的请求

{
"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

【问题讨论】:

  • 您的“找到”代码看起来像是笨拙地转换的 VBScript。但是 (a.) 你没有发送你的 JSON,并且 (b.) POST 通常期望 application/x-www-form-urlencoded 用于 Content-Type 这意味着你必须首先以这种方式格式化有效负载。为此,您需要知道服务器期望的元素名称,执行 UrlEncoding 等。
  • 当然服务器也可以接受原始 JSON,如果它会采用 application/json 代替。
  • @Bob77 我发现很少,我是 VB6 的新手 Set myMSXML = New MSXML.XMLHTTPRequest myMSXML.open "POST", URL, True myMSXML.setRequestHeader "Content-Type", "application/x- www-form-urlencoded" myMSXML.setRequestHeader "User-Agent", "Firefox 3.6.4" myMSXML.OnReadyStateChange =(如下所示)myMSXML.send YourPostDataString
  • @Bob77 你有可以帮助我的示例 VB6 代码吗?
  • 该对象不适用于 API 调用,因为它通过 UrlMon/IE 缓存工作。无论如何,换马不会改变你需要骑行的路径。

标签: vb6


【解决方案1】:

这可能是最简单的例子:

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

【讨论】:

  • 那行得通,现在我正在寻找 Json to Object 转换器。你有什么想法吗?
【解决方案2】:

对这里的答案不满意。 所以这里是一个 PUT 请求的简单示例。

Sub API_TEST()
'
Dim objWinHTTP
Set objWinHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")


    objWinHTTP.Open "PUT", "https://localhost:5001/api/?department=DDBB&changenumber=1337&isNew=1"
    objWinHTTP.SetTimeouts 5000, 5000, 5000, 5000


    Call objWinHTTP.Send(psData)
    MsgBox (objWinHTTP.Status)
    If objWinHTTP.Status <> 200 Then

    MsgBox ("Something went wrong.")

    End If
   
End Sub

【讨论】:

    猜你喜欢
    • 2011-12-19
    • 2012-03-04
    • 2013-02-20
    • 2010-09-07
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 2015-05-17
    相关资源
    最近更新 更多