从VBScript调用WebApi方法

我的控制器中有以下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(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().Result; } 

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

我尝试使用谷歌搜索,但我没有遇到任何从VB脚本调用web api方法的实例。

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

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

我制作了一个标准的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的清理版本

  1 MyApplication  

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