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

これは少し古いことはわかっていますが、解決しました。他の誰かがこのタスクに行き詰まった場合に備えて、答えを出すと思いました。新しい仕事で、vbscript で多くのことを行う SmarTeam という製品を使用する必要があるため、これが必要でしたが、より多くの機能が必要でした。

標準の 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>

パスの 2 番目の部分と、XML に飛び込む strQuery を変更することで、さまざまな API 呼び出しをテストできます。

于 2014-03-11T14:33:40.273 に答える