从VBScript调用WebApi方法 [英] Calling WebApi method from VBScript
问题描述
我的控制器中具有以下Web api方法
I have the following web api method in my controller
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控制台应用程序调用它的方式
This is how I'm calling it from a .NET console app
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进行相同的调用?
How is it possible to make the same call using VBScript?
我尝试使用谷歌搜索,但没有遇到从VB脚本调用Web api方法的可靠示例.
I tried googling but I haven't come across any solid examples of calling web api methods from VB script.
我的Web API方法是否支持来自VBScript的调用?还是需要一些调整?
Does my web api method support calls from VBScript? Or would I need some tweaking?
推荐答案
我知道这已经有点老了,但是我解决了.想通了,我会提出一个答案,以防其他人陷入该任务.我之所以需要它,是因为在一项新工作中,我必须使用一个名为SmarTeam的产品,该产品在vbscript中有很多功能,但是我们需要更多的功能.
I know this is a bit old now, but I solved it. Figured I would put up an answer in case anyone else gets stuck with this task. I needed it because at a new job I have to work with a product called SmarTeam, which does a lot in vbscript, but we needed more power.
我制作了一个标准的ASP.Net Web API2.请按照那里的所有教程进行操作.我的离这很近.
I made a standard ASP.Net Web API 2. Follow any tutorial out there to make one. Mine is pretty close to this.
接下来,我制作了一个vbscript文件,首先需要一些变量.我大部分是从几个不同的站点获得的,并且把它们放在一起,所以它是从几个不同的来源偷来的.
Next I made a vbscript file, first I need some variables. I got most of this from several different sites, and put it together, so it is stolen from a few different sources
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准备好与我们交谈的方法
Next is a method that sets up my objects, and makes sure the api is ready to talk to us
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(请求已完成,响应已准备就绪)
Now we need a method that will make sure the API is ready, and when it is, handle it. To understand the different ReadyState options check this link, but 4 is the only one we care about (request finished and response is ready)
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的清理版本
And for completion, here is a cleaned up version of the XML my API was returning
<Application>
<ID>1</ID>
<ApplicationName>MyApplication</ApplicationName>
</Application>
您可以通过更改路径的第二部分以及深入XML的strQuery来测试不同的API调用
You can test different API calls by changing the second part of the path, and the strQuery that dives into the XML
这篇关于从VBScript调用WebApi方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!