1

我的控制器中有以下 Web api 方法

    public HttpResponseMessage PostUpdateCardStatus(CardholderRequest cardholderRequest)
    {
        var cardId = cardholderRequest.CardId;

        switch (cardholderRequest.Action)
        {
            case "Enable":
                break;
            case "Disable":
                break;                
        }

        var cardholderResponse = new CardholderResponse(cardholderRequest.RequestId)
        {
            Status = "OK"
        };
        var response = Request.CreateResponse<CardholderResponse>(HttpStatusCode.OK, cardholderResponse);
        return response;
    }

这就是我从 .NET 控制台应用程序调用它的方式

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://localhost:55208/");

            var request = new CardholderRequest()
            {
                RequestId = Guid.NewGuid().ToString(),
                CardId = "123456",
                Action = "Enable",
                LoginId = "tester",
                Password = "tester",
            };
            var response = client.PostAsJsonAsync("api/cardholders", request).Result;
            if (response.IsSuccessStatusCode)
            {
                var cardholderResponse = response.Content.ReadAsAsync<CardholderResponse>().Result;

            }

如何使用 VBScript 进行相同的调用?

我试过谷歌搜索,但我没有遇到任何从 VB 脚本调用 web api 方法的可靠示例。

我的 web api 方法是否支持来自 VBScript 的调用?还是我需要一些调整?

4

1 回答 1

3

我知道这现在有点老了,但我解决了它。想我会提出一个答案,以防其他人被这个任务卡住。我需要它是因为在一份新工作中我必须使用一个名为 SmarTeam 的产品,它在 vbscript 中做了很多工作,但我们需要更多的功能。

我制作了一个标准的 ASP.Net Web API 2。按照那里的任何教程制作一个。我的非常接近这个。

http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

接下来我制作了一个 vbscript 文件,首先我需要一些变量。我从几个不同的站点获得了大部分内容,并将它们放在一起,所以它是从几个不同的来源窃取的

Dim oXMLDoc ' this will hold the response data
Dim oXMLHTTP ' this is the object that will request the data
const URLBase = "http://localhost:16370/api/" ' here is where my local web api was running
const AppSinglePath = "application/get/1" ' and this is just one of the paths available in my api

接下来是设置我的对象的方法,并确保 api 已准备好与我们交谈

Sub GetResponse
    Set oXMLHTTP = CreateObject("Microsoft.XmLHttp") ' create my request object
    Set oXMLDoc = CreateObject("MSXML2.DOMDocument") ' create my response object

    oXMLHTTP.onreadystatechange = getref("HandleStateChange") ' mode on this below, but it makes sure the API is ready
    Dim url = URLBase & AppSinglePath ' set up the URL we are going to request
    call oXMLHTTP.open("GET", url, false)
    call oXMLHTTP.setrequestheader("content-type","application/x-www-form-urlencoded")
    call oXMLHTTP.send()
End Sub

现在我们需要一个方法来确保 API 准备就绪,并在准备就绪时对其进行处理。要了解不同的 ReadyState 选项,请查看此链接,但我们只关心 4 (请求已完成且响应已准备好)

http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

Sub HandleStateChange
    If oXMLHTTP.readyState = 4 Then
        ' get the response
        Dim szResponse: szResponse = oXMLHTTP.responseText
        ' turn it into XML we can read
        call oXMLDoc.loadXML(szResponse)
        If oXMLDoc.parseError.errorCode <> 0 Then
            ' there was an error, tell someone
            call msgbox(oXMLDoc.parseError.reason)
        Else
            ' i was writing the response to a local file, because I wanted to see the XML
            Set oFile = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\APIResults\api.txt",2,true)
            oFile.WriteLine(oXMLDoc.xml)
            oFile.Close
            Set oFile = Nothing

            ' We need to make a query to dive into the XML with
            Dim strQuery = "/Application/ApplicationName"

            ' Now we need to rip apart the XML, and do whatever we want with it
            Set colNodes = oXMLDoc.selectNodes(strQuery)
            For Each objNode in colNodes
                WScript.Echo objNode.nodeName & ": " & objNode.text
            Next
        End If
    End If
End Sub

为了完成,这里是我的 API 返回的 XML 的清理版本

<Application>
    <ID>1</ID>
    <ApplicationName>MyApplication</ApplicationName>
</Application>

您可以通过更改路径的第二部分以及深入 XML 的 strQuery 来测试不同的 API 调用

于 2014-03-11T14:33:40.273 回答