C# 从VBScript调用WebApi方法

C# 从VBScript调用WebApi方法,c#,.net,web-services,vbscript,asp.net-web-api,C#,.net,Web Services,Vbscript,Asp.net Web Api,我的控制器中有以下web api方法 public HttpResponseMessage PostUpdateCardStatus(CardholderRequest cardholderRequest) { var cardId = cardholderRequest.CardId; switch (cardholderRequest.Action) { case "Enable":

我的控制器中有以下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;
    }
public HttpResponseMessage positionpdatecardstatus(持卡人请求持卡人请求)
{
var cardId=持卡人请求.cardId;
开关(持卡人请求操作)
{
案例“启用”:
打破
案例“禁用”:
打破
}
var CANDERRESPONSE=新持卡人响应(CANDERRequest.RequestId)
{
Status=“确定”
};
var response=Request.CreateResponse(HttpStatusCode.OK,响应);
返回响应;
}
这就是我在.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;

            }
使用(var-client=new-HttpClient())
{
client.BaseAddress=新Uri(“http://localhost:55208/");
var请求=新持卡人请求()
{
RequestId=Guid.NewGuid().ToString(),
cardd=“123456”,
Action=“Enable”,
LoginId=“测试仪”,
Password=“tester”,
};
var response=客户端.PostAsJsonAsync(“api/持卡人”,请求)。结果;
if(响应。IsSuccessStatusCode)
{
var-response=response.Content.ReadAsAsync().Result;
}
如何使用VBScript进行相同的调用

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


我的web api方法支持VBScript调用吗?或者我需要一些调整吗?

我知道这有点旧,但我解决了它。我想我会给出一个答案,以防其他人被困在这项任务中。我需要它,因为在一份新工作中,我必须使用一个名为SmarTeam的产品,它在VBScript中发挥了很多作用,但我们需要更多奥威尔

我制作了一个标准的ASP.NETWebAPI2。按照任何教程制作一个。我的非常接近这个

接下来,我制作了一个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(请求已完成,响应已准备就绪)

子手柄状态更改
如果oXMLHTTP.readyState=4,则
”他说
Dim szResponse:szResponse=oXMLHTTP.responseText
'将其转换为我们可以阅读的XML
调用oXMLDoc.loadXML(szResponse)
如果oXMLDoc.parseError.errorCode为0,则
“有一个错误,告诉别人
调用msgbox(oXMLDoc.parseError.reason)
其他的
'我正在将响应写入本地文件,因为我想查看XML
Set of ile=CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\apitresults\api.txt”,2,true)
oFile.WriteLine(oXMLDoc.xml)
奥菲尔,关门
文件集=无
“我们需要做一个查询来深入研究XML
Dim strQuery=“/Application/ApplicationName”
“现在我们需要拆开XML,并对它做任何我们想做的事情
设置colNodes=oXMLDoc.selectNodes(strQuery)
对于colNodes中的每个长方体节点
WScript.Echo objNode.nodeName&“:”&objNode.text
下一个
如果结束
如果结束
端接头
最后,这里是我的API返回的XML的一个清理版本

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

1.
我的申请

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

您知道这一点吗?我刚刚接到同样的任务。不,从来没有想过。
<Application>
    <ID>1</ID>
    <ApplicationName>MyApplication</ApplicationName>
</Application>