.net 在VBScript中调用WebApi方法

gr8qqesn  于 2023-03-13  发布在  .NET
关注(0)|答案(2)|浏览(252)

我的控制器中有以下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调用吗?或者我需要一些调整吗?

7lrncoxx

7lrncoxx1#

我知道这个问题有点老了,但我解决了它。我想我应该给出一个答案,以防其他人陷入这个任务。我需要它,因为在一份新工作中,我必须使用一个名为SmarTeam的产品,它在vbscript中做了很多工作,但我们需要更强大的功能。
我做了一个标准的API。遵循任何教程有一个。我的是相当接近这一点。
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调用

yqlxgs2m

yqlxgs2m2#

这是一个很长的时间,但感谢您的职位!

相关问题